Assigning numOnes with Modulo Operator

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Operator
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 15K views
ineedhelpnow
Messages
649
Reaction score
0
A cashier distributes change using the maximum number of five dollar bills, followed by one dollar bills. For example, 19 yields 3 fives and 4 ones. Write a single statement that assigns the number of distributed 1 dollar bills to variable numOnes, given amountToChange. Hint: Use %.

Sample program:

#include <iostream>
using namespace std;

int main() {
int amountToChange = 0;
int numFives = 0;
int numOnes = 0;

amountToChange = 19;
numFives = amountToChange / 5;
<STUDENT CODE>

return 0;
}


i only have to write code for the part that says <student code>. i tried numOnes = 19 % 5;

its wrong though. it passed two tests but failed one.
 
Last edited:
Physics news on Phys.org
oh i got it. it was supposed to be amountToChange % 5 not 19 % 5 because then it only works for one value
 
To which variable are you applying the modulus operator?
 
ineedhelpnow said:
oh i got it. it was supposed to be amountToChange % 5 not 19 % 5 because then it only works for one value

Good! (Yes)

That was my suspicion, that you were applying "% 5" to the wrong value. :D