Computer Science, asked by kchvsrAnu8407, 11 months ago

Write a python program to solve a classic ancient chinese puzzle.We count 35 heads and 94 legs among the chickens and rabbits in a farm. How many rabbits and how many chickens do we have?

Answers

Answered by SerenaBochenek
1

A python program to solve a classic ancient Chinese puzzle is given below.

Output:

The number of chickens are 23 and the number of rabbits are 12.

The biggest brain gets brainliest.

Explanation:

#main method with two arguments

def main(t, n_leg):

#for loop to iterate over the condition

for rabbits in range(t + 1):

#calculating the chickens

chickens = t - rabbits

#if statement to check the legs of chickens and rabbits

if 2 chickens + 4 rabbits == n_leg:

#return chickens and rabbits

return chickens, rabbits

#if statement will check whether the current script is being matched

if _name_ == '__main__':

#declaring the number of heads

n_head = 35

#declaring the number of heads

n_leg = 94

#res variable to store arguments of main function

res = main(n_head, n_leg)

print("The number of chickens are %d and the number of rabbits are %d." % res)

The main() function is defined with two arguments. The first argument will store total number of chickens and the second argument will store the total number of legs. 

For loop is used to iterate over the condition. And to calculate the number of chickens and rabbits. Inside the loop the if statement will return the number of chickens and rabbits if the total no. of chicken and rabbits is equal to total no. of legs

Now, the if statement will check whether the current script is being matched also declaring the number of heads and legs.

The variable res will store the number of heads and the number of legs.

The print() function will print the number of heads and the number of legs. 

Learn more:

Write a python program to input heads count and legs count and print the count of rabbits and count of chickens.

if the input is not suitable to print count of rabbits and count of chickens then the output should be "No solution".

https://brainly.in/question/10498755

Answered by Anonymous
0

Answer:

#chicken and rabbit problem

l=int(input("Number of legs"))

h=int(input("Number of heads"))

if l>h and h!=0 and l!=0:

   if (l-2*h)%2!=0:

         print("Not possible")

   else:

       r=(l-2*h)/2

       c=h-r

       print("Number of rabbit=",int(r))

       print("Number of chicken=",int(c))

else:

   print("Not possible")

Explanation:

let 'l' be the number of legs and 'h' be the number of heads

and 'r' be number of rabbit and 'c' be number of chicken

so r+c=h

and 4r+2c=l

solving both the equations we get

r=(l-2h)/2

and c=h-r

Similar questions