In: Computer Science
c++ code problems : A farmer buys 100 animals for $100.00. The animals include at least one cow, one pig, and one chicken, but no other kind.
If a cow costs $10.00, a pig costs $3.00, and a chicken costs $0.50, how many of each did he buy?
please using a loop.
C++ code with complete explanation in the comments:
#include
using namespace std;
int main()
{
int max_cow=100/10;//max number of cows that can be
bought in 100 dollar
int max_pig=100/3;//max number of pigs that can be
bought in 100 dollar
for(int i=1;i<=max_cow;i++)//to keep count of
cows
{
for(int j=1;j<=max_pig;j++)//to
keep count ofpigs
{
int
cost=i*10+j*3;
int remaining
=100-i-j;//number of chickens
if(remaining%2==0)//this is because if chickens are in odd number
then their cost will become fraction
{//and as it
should be integer
int costofchicken=remaining/2;
cost+=costofchicken;
if(cost==100)
cout<
}
}
}
return 0;
}
Output: