# Count Number of animals in a zoo from given number of head and legs

Given the total number of Legs and Head of Rabbits and Pigeons. The task is to calculate the number of Rabbits and Pigeons.

**Examples:**

Input:Heads = 200, Legs = 540Output:Rabbits = 70, Pigeons = 130Input:Heads = 100, Legs = 300Output:Rabbits = 50, Pigeons = 50

Let the total no. of Heads = 200 and Legs = 540.

Let the number of Head and Legs of Rabbits = X and that of Pigeons = Y.

so,

X + Y = 200 ..equation 1(no. of the heads of a rabbit and a pigeon = total no. of heads)

(As both of them has 1 head)

4X + 2Y = 540 …equation 2(no. of the legs of the rabbit and a pigeon = total no. of legs)

(Rabbit have 4 legs and pigeons have 2 legs)

Now, solving equation 1 and 2 we get,

4X = 540 – 2Y

4X = 540 – 2 * (200 – X)

4X = 540 – 400 + 2X

2X = 140 => X = 70

X = 70 and Y = 130.

hence, Rabbits = 70 and Pigeons = 130

Below is the implementation of the above approach:

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that calculates Rabbits ` `int` `countRabbits(` `int` `Heads, ` `int` `Legs) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `count = (Legs)-2 * (Heads); ` ` ` `count = count / 2; ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `Heads = 100, Legs = 300; ` ` ` ` ` `int` `Rabbits = countRabbits(Heads, Legs); ` ` ` ` ` `cout << ` `"Rabbits = "` `<< Rabbits << endl; ` ` ` `cout << ` `"Pigeons = "` `<< Heads - Rabbits << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` `import` `java.util.*; ` `import` `java.lang.*; ` ` ` `class` `GFG ` `{ ` `// Function that calculates Rabbits ` `static` `int` `countRabbits(` `int` `Heads, ` ` ` `int` `Legs) ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `count = (Legs) - ` `2` `* (Heads); ` ` ` `count = count / ` `2` `; ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `Heads = ` `100` `, Legs = ` `300` `; ` ` ` ` ` `int` `Rabbits = countRabbits(Heads, Legs); ` ` ` ` ` `System.out.println(` `"Rabbits = "` `+ ` ` ` `Rabbits); ` ` ` `System.out.println(` `"Pigeons = "` `+ ` ` ` `(Heads - Rabbits)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai(Abby_akku) ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of above approach ` ` ` `# Function that calculates Rabbits ` `def` `countRabbits(Heads, Legs): ` ` ` `count ` `=` `0` ` ` ` ` `count ` `=` `(Legs) ` `-` `2` `*` `(Heads) ` ` ` `count ` `=` `count ` `/` `2` ` ` ` ` `return` `count ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `Heads ` `=` `100` ` ` `Legs ` `=` `300` ` ` ` ` `Rabbits ` `=` `countRabbits(Heads, Legs) ` ` ` ` ` `print` `(` `"Rabbits ="` `, Rabbits) ` ` ` `print` `(` `"Pigeons ="` `, Heads ` `-` `Rabbits) ` ` ` `# This code is contributed ` `# by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// Function that calculates Rabbits ` `static` `int` `countRabbits(` `int` `Heads, ` ` ` `int` `Legs) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `count = (Legs) - 2 * (Heads); ` ` ` `count = count / 2; ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `Heads = 100, Legs = 300; ` ` ` ` ` `int` `Rabbits = countRabbits(Heads, Legs); ` ` ` ` ` `Console.WriteLine(` `"Rabbits = "` `+ ` ` ` `Rabbits); ` ` ` `Console.WriteLine(` `"Pigeons = "` `+ ` ` ` `(Heads - Rabbits)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai(Abby_akku) ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of above approach ` ` ` `// Function that calculates Rabbits ` `function` `countRabbits(` `$Heads` `, ` `$Legs` `) ` `{ ` ` ` `$count` `= 0; ` ` ` ` ` `$count` `= (` `$Legs` `) - 2 * (` `$Heads` `); ` ` ` `$count` `= (int) ` `$count` `/ 2; ` ` ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver code ` `$Heads` `= 100; ` `$Legs` `= 300; ` `$Rabbits` `= countRabbits(` `$Heads` `, ` `$Legs` `); ` ` ` `echo` `"Rabbits = "` `, ` `$Rabbits` `, ` `"\n"` `; ` `echo` `"Pigeons = "` `, ` `$Heads` `- ` ` ` `$Rabbits` `, ` `"\n"` `; ` ` ` `// This code is contributed ` `// by Sach_Code ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Rabbits = 50 Pigeons = 50

## Recommended Posts:

- Number of sequences which has HEAD at alternate positions to the right of the first HEAD
- Count number of triplets with product equal to given number with duplicates allowed
- Count number of trailing zeros in Binary representation of a number using Bitset
- Find count of digits in a number that divide the number
- Count number of digits after decimal on dividing a number
- Count the number of operations required to reduce the given number
- Count number of ways to divide a number in 4 parts
- Count number of subsets of a set with GCD equal to a given number
- Count the number of occurrences of a particular digit in a number
- Count number of rotated strings which have more number of vowels in the first half than second half
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Count the number of possible triangles
- Count Hexadecimal Number
- Count number of ways to get Odd Sum

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.