Breaking

Sunday, 17 September 2017

Head Count - Birds and Animals

                    Head Count - Birds and Animals


code for Head count birds and animals

                                                   

In a zoo there are some birds and animals. All birds have two legs and all animals have four legs.
GIven the head count and leg count of both birds and animals taken together, the program must print the head count of birds and animals separated by a space as output.
Input Format:
First line will contain the integer value H representing the head count of both birds and animals taken together.
Second line will contain the integer value L representing the leg count of both birds and animals taken together.
Output Format:
First line will contain the integer values of the head count of birds and animals separated by a space.
Constraints:
0 < H < 1000
1 < L < 2000
Sample Input/Output:
Example 1:
Input:
27
84
Output:
12 15
Explanation:
There are 12 birds and 15 animals.
Example 2:
Input:
114
256
Output:
100 14
Explanation:
There are 100 birds and 14 animals.

Code:

Logic 1:
#include<iostream>
using namespace std;

int main(int argc, char** argv)
{
int a,b;
cin>>a>>b;
cout<<abs((b-(4*a))/2)<<" ";
cout<<abs((b-(2*a))/2);
}


Logic 2:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int head,leg,i,j,flag=0;
cin>>head>>leg;

for(i=1;i<head;i++)
{
    for(j=leg;j>1;j--)
    {
        if(leg==(i*2)+(j*4))
        {
            if(head==i+j)
            {
                cout<<i<<" "<<j;
                flag=1;
                break;
            }
        }
    }
    if(flag==1)
        break;
}
}

Please do comment If u have any Queries!

2 comments:

  1. #include
    #include
    using namespace std;

    int main(int argc, char** argv)
    {
    int a,b;
    cin>>a>>b;
    cout<<abs((b-(4*a))/2)<<" ";
    cout<<abs((b-(2*a))/2);
    }

    ReplyDelete
  2. adaiii java la kudunga da

    ReplyDelete

Like