Python @ DjangoSpin

PyPro #38 Cows & Chickens

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Cows & Chickens in Python

Cows & Chickens in Python

Cows & Chickens: There are 63 animals in a farm which has only chickens and cows. Leg count is 206. Given that chickens have 2 legs and cows have 4, write a Python program to figure out the number of chickens and cows there are in the farm. Hint: A simple for loop should do the trick.

Cows & Chickens

totalNumberOfAnimals = 63
totalNumberOfLegs = 206

for numberOfChickens in range(totalNumberOfAnimals + 1):
    numberOfCows = totalNumberOfAnimals - numberOfChickens
    if 2 * numberOfChickens + 4 * numberOfCows == totalNumberOfLegs:
        print("Number of Chickens: {}".format(numberOfChickens))
        print("Number of Cows: {}".format(numberOfCows))

See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply