Write a program that will calculate the types of coins that will add up to a Dollar cents number that is input by the user. For example if the user inputs 1.50, the program should output “6 quarters are equal to $1.50”. Take into account, quarters, dimes, nickels and pennies. As another example if the user inputs 2.12, the program should ouput “8 quarters, 1 dime and 2 pennies are equal to $2.12”.
Your program should give the minimum number of coins that will add up to the total amount, i.e. Your output for $1.50 should be 6 quarters and not 15 dimes or 30 nickels.
for some reason when i input certain values like .95 it doesn't set dimes to 2 but sets it to 1 instead. i can't figure out it not working correctly. PLZ help
Code:
#include <stdio.h>
#include <math.h>
int main()
{
double money,money2;
int quarter=0,dime=0,nickle=0,cent=0;
printf("Input the ammount of money: ");
scanf("%lf",&money);
money2=money;
printf("%lf \n",money2);
if(money2/.25>=1)
{
money2=money2/.25;
quarter=(int) money2;
money2=money-quarter*.25;
printf("%lf \n",money2);
}
if(money2/.1>=1)
{
money2=money2/.1;
dime= money2;//******************
money2=money-quarter*.25-dime*.1;
printf("%lf \n",money2);
}
if(money2/.05>=1)
{
money2=money2/.05;
nickle=(int)money2;
money2=money-quarter*.25-dime*.1-nickle*.05;
printf("%lf \n",money2);
}
if(money2/.01>=1)
{
money2=money2/.01;
cent=(int)money2;
money2=money-quarter*.25-dime*.1-nickle*.05-cent*.01;
printf("%lf \n",money2);
}
printf("%d quarters\n",quarter);
printf("%d dimes\n",dime);
printf("%d nickles\n",nickle);
printf("%d cents\n",cent);
}