Let's go to logic, for every 1 head there are 2 or 4 legs.
2 legs = duck.
Home
4 legs = rabbit.
The numbers should hit, for example, it does not make sense to have 10 heads and 200 legs, after all 10x4 = 40, this being the maximum possible of legs per head.
There is also the minimum possibility, as 10 heads can have 20 legs.
Both possibilities being true you can continue your program, you have to implement more things, but you would say that this is a principle, you have to limit the possibilities to be within your goals.
I made in C a basic test, the logic in any language is the same:
int main(int argc, char** argv) {
int totalCabecas;
int totalPatas;
printf("Digite o Total de Cabeças: ");
scanf("%d", &totalCabecas);
printf("Digite o Total de Patas: ");
scanf("%d", &totalPatas);
int testeMax = (totalCabecas) * 4; // 10x4 = 40, maximo de patas possiveis
int testeMin = (totalCabecas) * 2; // 10x2 = 20, minimo de patas possiveis
if ((totalPatas > testeMax) || (totalPatas < testeMin)) {
printf("ERRO\n");
} else {
printf("OK!\nPossibilidade aceita\n");
}
return (EXIT_SUCCESS);
}