fairly fresh coder that is learning to create a program that will output the fewest coins possible from any given cent input between 0 and 99. I don't need to add any limits to this code which is why you don't see anything limiting between 0 and 99.
Here is what I have, but I can't seem to figure out how to properly use a modulus to carry over the leftover number from the previous calculation.
I appreciate any advice you can offer! I think I am pretty close, but am at a mental wall with the % modulus. I know I can calculate the previous number using long arithmetic but I would like to figure out how to make it shorter with the % modifier.
#include <iostream>
using namespace std;
int main()
{
int cents;
const int quarter = 25;
const int dime = 10;
const int nickel = 5;
const int penny = 1;
// Get the amount of cents
cout << "Please enter an amount in cents less than a dollar." << endl;
cin >> cents;
// Calculate how many of each coin you can get from cent input
int Q = cents % quarter;
int D = Q % dime;
int N = D % nickel;
int P = N % penny;
// Display the coins you can get from cent input
cout << "Your change will be:" << endl;
cout << "Q: " << Q << endl;
cout << "D: " << D << endl;
cout << "N: " << N << endl;
cout << "P: " << P << endl;
return 0;
}
%
returns the remainder of the division, not the rounded figure. So it is assigning to Q
the remainder of the divison. Id suggest you first calculate the number of X
type coin, and then pass the remainder on to the next calculation. Something like
int Q = cents / quarter;
int D = (cents%quarter) / dime;
And so on