The need:
The purpose of writing this program was to solve a mathematical riddle which once somebody asked me when I was in 9th class. At that time it took a long time of mine to solve it. Once when I was practicing C I thought that I could write a program that could solve it for me so I wrote this program.The riddle is:
The purpose of writing this program was to solve a mathematical riddle which once somebody asked me when I was in 9th class. At that time it took a long time of mine to solve it. Once when I was practicing C I thought that I could write a program that could solve it for me so I wrote this program.The riddle is:
There are three types of pots available in the market. The costs are as under:
1. one item of Type1 costs 5 bucks.
2. one item of Type2 costs 1 buck.
3. twenty items of Type3 cost 1 buck.
If someone wants to purchase 100 items for 100 bucks what selection of pots should he make?
--------------------------------------------
//The pot riddle
#include<stdio.h>
main()
{
float i,j,k;
for(i=1;i<=100;i++)
for(j=1;j<=100;j++)
for(k=1;k<=100;k++)
if((i+j+k==100)&&(5*i+j+.05*k==100))
{
printf("number of type1 pots =>\t%.0f\n",i);
printf("number of type2 pots =>\t%.0f\n",j);
printf("number of type3 pots =>\t%.0f\n",k);
//break;
}
}
--------------------------------------------
The approach:
The approach used here is a layman's approach. This program starts checking for every possible combination of pots whether the given condition is satisfied or not. This is done with the help of 3 nested loops. The 3 variables i,j,k represent the numbers of type1, type2 and type3 pots respectively. Whenever the the condition is met, the corresponding values of i,j,k get printed on the screen.
The approach:
The approach used here is a layman's approach. This program starts checking for every possible combination of pots whether the given condition is satisfied or not. This is done with the help of 3 nested loops. The 3 variables i,j,k represent the numbers of type1, type2 and type3 pots respectively. Whenever the the condition is met, the corresponding values of i,j,k get printed on the screen.
Remarks:
There are 2 noticeable points.
1. The break; statement which has been commented here. If it was not commented, the program would find a solution and exit. The purpose of commenting is to check for more than one solution if existed.
2. "%.0f" is used for printing float values instead of "%f". For that, let me tell you, when you print a float number by using "%f", the output number has 6 digits after the decimal point. Here I wanted to tell that we can control the number of places after the decimal point in the output.
if I write "%.nf" then then the output has n digits after decimal.( replace n with any non negative integer like here I have used 0 because I did not want answer in decimal.)
No comments:
Post a Comment
feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply