I am writing a program, (using strictly C) which calculates the minimum change in US denominations. After my program reaches dimes (the first denomination that is not divisible by a whole number the program seems to not want to continue to nickels and pennies.
I have tried to use an if statement, excluding dimesDue value if returning a zero, but cant seem to figure it out. If you notice, I also had to make a new change variable for each denomination, based on the previous denominations deduction from the total change amount. I would prefer to simplify this and specify the new value after each calculation, but could not.
// Amount Tendered and Purchase amount converted to pennies
amountDue = 2117;
amountGiven = 10000;
// Creating a new change amount for each denomination, based on each previous computation
change = amountGiven - amountDue;
change10s = change % (20 * 100);
change5s = change % (10 * 100);
change1s = change % (5 * 100);
changeQs = change % (1 * 100);
changeDs = change % 25;
changeNs = change % 10;
changePs = change % 1;
// Using each new change amount to calculate amount of denomination
twentiesDue = (change / 20) / 100;
tensDue = (change10s / 10) / 100;
fivesDue = (change5s / 5) / 100;
onesDue = (change1s / 1) / 100;
quartersDue = (changeQs / 25);
dimesDue = (changeDs / 10);
nickelsDue = (changeNs / 5);
penniesDue = (changePs / 1);
printf("Amount Due: $21.17\nAmount Tendered: $100\n\n");
printf("Change Due:\n(by denomination)\n");
printf("Twenties: %d\n", twentiesDue);
printf("Tens: %d\n", tensDue);
printf("Fives: %d\n", fivesDue);
printf("Ones: %d\n", onesDue);
printf("Quarters: %d\n", quartersDue);
printf("Dimes: %d\n", dimesDue);
printf("Nickels: %d\n", nickelsDue);
printf("Pennies: %d\n", penniesDue);
The program reaches dimes (the first denomination that does not equal a whole number) and does not continue on to caculate the amount of nickels and pennies. Because the remainder of change left after quarters is 8 cents, this is not divisible by 10. but I cant figure out how to specify to ignore this using a if statement!
So the result is once the program reaches dimes, all variable thereafter compute to zero. But there should be one nickel and three pennies! Here is the result when I run it:
Amount Due: $21.17 Amount Tendered: $100
Change Due: (by denomination) Twenties: 3 Tens: 1 Fives: 1 Ones: 3 Quarters: 3 Dimes: 0 Nickels: 0 Pennies: 0
Aucun commentaire:
Enregistrer un commentaire