We count 35 heads and 94 legs among the chickens and rabbits in a ground. How many rabbits and how many chickens do we have? //this is an example for program
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".
please refer to the pic i gave to know about more of how to get output and how to not to get an output examples....
The biggest brain gets brainliest
Answers
Alright so! Let's understand the basic concept:
We are given the total number of heads and legs. The total number of heads indicate the total number of animals. Because, each animal has one head.
Now, coming to the legs, a chicken has 2 legs, where as a rabbit has 4 legs. Here is the tricky part, sometime, this wouldn't work and we have to print 'No solution'.
Now, we need a a line which prints No Solution. Here is how:
if legs % 2 !=0 or heads ==0 or heads > legs:
print('No Solution')
So what this code says is the remainder '%' of legs and 2 is not equal to '!=' 0 or if heads is equal to 0 or if the number of heads is greater than legs. Cause for 1 rabbit and 1 chicken, we will have two heads but 6 legs. So, the number of legs should always be greater than the number of heads. If all these criteria matches.Then, print No Solution. Because we have two animals who have legs in an even count. 2 and 4. If we add them up, for 1 chicken and 1 rabbit, we will have 6 legs, for 2, we will have 12 and for 3 18. If this trend continues, we have all even numbers. So, there is no chance that the legs should be an odd number.
Before this, we will have to take input. Remember, the input we take should be the integer type. Because we accept only numbers!
This is the input step:
heads = int(input('Enter the total number of heads:'))
legs = int(input('Enter the total number of legs:'))
After we have done, here comes the mathematical part.
Let's the number of rabbits as 'r' and number of chickens as 'c'. We can easily form a two variable equation.
r + c = heads
The number of rabbits + number of chickens = the number of heads.
Remember, in python, the '=' sign should go first!
And now, 4r + 2c = legs
Rabbit has 4 legs and a chicken has 2 legs.
_______________________________
r + c = heads
4r + 2c = legs
___________________________-
Multiply the first equation with '-2'
-2r - 2c = -2heads
4r + 2c = legs
______________________
2r = legs - 2heads
Now, we can easily find the number of rabbits:
r = (legs - 2heads)/2
Easy! Now, we just paste the last line to find the number of rabbits.
Now that we have found the number of rabbits, we have to find the number of chickens. If you give a thought to the earlier statements.
Number of rabbits + Number of chickens = heads
Number of rabbits is 'r'. If you substitute:
r + number of chickens = heads
r + c = heads
c = heads - r
Number of chickens = heads - number of rabbits.
YES! We got it all figured out and now we have to write the code for this:
r = int((legs + (-2*heads))/2)
c = int(heads - r)
Now that we have the code, we will need to print the number of rabbits and number of chickens.
We can do this in three ways :
1) print(r,c). Because we need a comma -- This is the simplest way
2) print((r,c),sep = ' ') We print them but separate them with a comma.
3) print('{},{}'.format(c,r)) -- complex way but very useful in upper level programming.
Now, the final code:
heads = int(input('Enter the total number of heads:'))
legs = int(input('Enter the total number of legs:'))
if legs % 2 !=0 or heads ==0 or heads > legs:
print('No solution')
else:
r = int((legs + (-2*heads))/2)
c = int(heads - r)
print('{} {}'.format(c,r))
___________________________
Input :
1) 150 , 400
2) 3, 11
3) 3, 12
4) 5, 10
Output:
1) 100 50
2) No solution
3) 0 3
4) 5 0
___________________________
Answer:
import math
heads=35
legs=94
rabbit=legs/2-heads
chicken=heads-rabbit
if(rabbit==math.floor(rabbit) and chicken==math.floor(chicken)):
print(chicken,rabbit)
else:
print("No solution")
Explanation:
Hope it helps....
if rabbit is r and chicken is c
head count is 1 for each animal
so,r+c=head
and legs count is 4 for each rabbit and 2 for each chicken
so,2c+4r=leg
c+2r=leg/2
subtracting the equations,we get
r=leg/2-head
c=head-r
just putting equations in programming