Reputation: 11
This program is designed to take user input (amount of money) and output the input in coins. The program attempts to use the smallest amount of coins possible. The program works for the most part. When I enter a value of 2.30 for example, the out that I get is 9 quarters only. I need help in resolving this coding issue please.
#include <stdio.h>
#include <math.h>
int main()
{
int coins, quarter, money3, money2, pennies, nickels, dimes, quarters;
int penny, nickel, dime, money1;
float money;
quarter = 25;
dime = 10;
nickel = 5;
penny = 1;
printf("How much money would you like in change?\n");
scanf("%f", & money);
coins = (money * 100) + .5;
quarters = coins/quarter;
money1 = coins % quarter;
dimes = money1/dime;
money2 = coins % dime;
nickels = money2/nickel;
money3 = coins % nickel;
pennies = money3/penny;
printf("The number of quarters is %d\n", quarters);
printf("The number of dimes is %d\n", dimes);
printf("The number of nickels are %d\n", nickels);
printf("The number of pennies is %d\n", pennies);
printf("%d\n", money1);
printf("%d\n", coins);
}
Upvotes: 1
Views: 131
Reputation: 106082
Do not use the original value of coins
to calculate money2
and money3
. Instead money2
can be calculated by using money1
and money3
can be calculated by using money2
. You should change
money2 = coins % dime;
nickels = money2/nickel;
money3 = coins % nickel;
to
money2 = money1 % dime;
nickels = money2/nickel;
money3 = money2 % nickel;
Now 2.30
is giving the output:
The number of quarters is 9
The number of dimes is 0
The number of nickels are 1
The number of pennines is 0
5
230
Upvotes: 1
Reputation: 74675
Assuming that you want to divide the input value into the highest value coins possible, then you can work your way through the coins in descending order of value, subtracting the amount as you go. This removes the need for some of your variables.
How about this:
#include <stdio.h>
/* you don't actually need math.h for this code */
int main()
{
int coins, pennies, nickels, dimes, quarters;
int quarter, nickel, dime;
float money;
quarter = 25;
dime = 10;
nickel = 5;
printf("How much money would you like in change?\n");
scanf("%f", & money);
coins = (money * 100) + .5;
quarters = coins/quarter;
coins -= quarters * quarter;
dimes = coins/dime;
coins -= dimes * dime;
nickels = coins/nickel;
coins -= nickels * nickel;
pennies = coins;
printf("The number of quarters is %d\n", quarters);
printf("The number of dimes is %d\n", dimes);
printf("The number of nickels are %d\n", nickels);
printf("The number of pennies is %d\n", pennies);
return 0;
}
Upvotes: 1
Reputation: 40145
quarters = coins/quarter;
money1 = coins % quarter;
dimes = money1/dime;
money2 = money1 % dime;
nickels = money2/nickel;
money3 = money2 % nickel;
pennies = money3/penny;
Upvotes: 0
Reputation: 24259
You should either step thru the program with a debugger and inspect the values being assigned to variables as they go or add some more printf's to output the values of the variables as they are assigned/changed. The problem you have is a simple typo, basically. I recommend you have another go at trying to solve it and if you don't figure it out for yourself, see the following ideone live demo: http://ideone.com/BsIgg0
#include <stdio.h>
#include <math.h>
int main()
{
int coins, quarter, money3, money2, pennies, nickels, dimes, quarters;
int penny, nickel, dime, money1;
float money;
quarter = 25;
dime = 10;
nickel = 5;
penny = 1;
printf("How much money would you like in change?\n");
scanf("%f", & money);
coins = (money * 100) + .5;
printf("coins = %d\n", coins);
quarters = coins/quarter;
money1 = coins % quarter;
printf("money1 = %d\n", money1);
dimes = money1/dime;
money2 = coins % dime;
printf("money2 = %d\n", money2);
nickels = money2/nickel;
money3 = coins % nickel;
printf("money3 = %d\n", money3);
pennies = money3/penny;
printf("The number of quarters is %d\n", quarters);
printf("The number of dimes is %d\n", dimes);
printf("The number of nickels are %d\n", nickels);
printf("The number of pennies is %d\n", pennies);
printf("%d\n", money1);
printf("%d\n", coins);
}
Upvotes: 1