I need to write a program that will calucate how many quarters, dimes, nickels, and pennies a certain amount of cents will have. So for example, you enter 90 cents. That would be 3 quarters, 1 dime, 1 nickel, 0 pennies. I have this so far, im having trouble with the MATH part. The only thing working is the quarters.
#include %26lt;iostream.h%26gt;
main()
{
int quarters;
int dimes;
int nickels;
int pennies;
int number;
cout %26lt;%26lt; "Enter a number. " %26lt;%26lt; '\n';
cin %26gt;%26gt; number;
quarters = number / 25;
dimes = number - (quarters * 25) / 10;
nickels = number - (quarters * 25) / (dimes * 10) / 5;
pennies = number - (quarters * 25) / (dimes * 10) / (nickels * 5);
cout %26lt;%26lt; "Quarters - " %26lt;%26lt; quarters %26lt;%26lt; '\n';
cout %26lt;%26lt; "Dimes - " %26lt;%26lt; dimes %26lt;%26lt; '\n';
cout %26lt;%26lt; "Nickels - " %26lt;%26lt; nickels %26lt;%26lt; '\n';
cout %26lt;%26lt; "Pennies - " %26lt;%26lt; pennies %26lt;%26lt; '\n';
cout %26lt;%26lt; '\n';
system("PAUSE");
return 0;
}
Change C++ Program?
OK, let's break this up a bit:
Quarters
-----------
Your calculation for quarters was fine.
Dimes
----------
You must not forget the order of operations for mathematical computations. Division happens before subtraction. Try something more like this:
dimes = (number - (quarters * 25)) / 10;
Nickels
----------
You should add together the number of cents present in the quarters and dimes before subtracting them from the number, like this:
nickels = (number - ((quarters * 25) + (dimes * 10)) / 5;
Pennies
------------
For pennies, just subtract out the number of cents used in the quarters, dimes, and nickels.
pennies = number - ((quarters * 25) + (dimes * 10) + (nickels * 5));
That's my suggestion. Hope this helps. As an alternative, you could get the modula of the nickels calculation to get the pennies as well, like so:
pennies = (number - ((quarters * 25) + (dimes * 10)) % 5;
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment