I've put together a solution to the problem set Cash using division. When I run the program, it returns incorrect number of coins for most of the cases, e.g. change 61 - 244 coins, change 0.80 - 14 coins but it returns a correct value for change 0.25 (1 coin). I would be grateful for any hints.

#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void)
    int coins;
    float change;

    // Get user's input
        change = get_float("What is the change? ");
    // Condition for the input
    while (change < 0);

// Round dollars to cents
change = round(change * 100);

// Calculate coins

    int q = change / 25;
    int d = (change - (25 * q)) / 10;
    int n = (change - ((25 * q) + (10 * d))) / 5;
    int p = (change - (25 * q) + (10 * d) + (5 * n)) / 1;
    coins = q + d + n + p;

printf("Number of used coins: %i\n", coins);
  • If that approach is not working for you, maybe you could try with some if, else if and else conditional solution. Working with them will help you to get comfortable for using them in the future.
    – Tritum
    Apr 27, 2020 at 20:58
  • Observe the difference between the parenthetical expressions for d and n compared to p. Apr 27, 2020 at 22:01
  • Thanks for the hint. How could I have missed it!
    – MarkB
    Apr 28, 2020 at 9:44
  • @Tritum I know I should try and solve it with if, else if rather than basic maths. I tried but it didn't work at all :/
    – MarkB
    Apr 28, 2020 at 9:44

1 Answer 1


It's an efficient approach and you're on the right track! The problem is that it's calculating the total number of each type of coin required, but not accounting for the previous calculations. [EDIT: oops, wrong!]

Simply put, after accounting for the total number of quarters, the code needs to subtract the value of the quarters from the total before calculating dimes, and so on.

[EDIT: corrected answer] Rafael perceived that there was an error in my analysis in his comment, although maybe not seeing the real problem. I was in error in my original analysis.

After reviewing it, I discovered that the true problem lies in the following code:

int p = (change - (25 * q) + (10 * d) + (5 * n)) / 1;

This line of code isn't calculating the correct number of pennies! It's supposed to subtract the value of quarters, dimes and nickels to see how many pennies remain. The problem is that there should be a set of parentheses around those three sections. Since there isn't, it subtracts the value of quarters, but then ADDS the value of dimes and nickels! OOPS! That's the real problem and my bad for not seeing it.

While it's a creative technique, and would be correct without that error, it shows that creative isn't necessarily good. A good rule of thumb in programming is to keep it simple. "Creative" often leads to the introduction of errors, just like this! It would have been both simpler and more obvious to just keep a running total in change to avoid complication. The simpler code would have been to do this:

int q = change / 25;
change -= 25 * q
int d = change  / 10;
change -= 10 * d
int n = change / 5;
int p -= change ;
coins = q + d + n + p;

The logic is this: calculate the number of a given type of coin, then subtract the value of those coins from the total. Repeat as needed. This also would require about 10 operations vs. 15 or 16 in the original code.

The point is this. Simpler is almost always better!

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Thanks, I adjusted my calculations and the code was accepted. Assignment submitted :) By another problem you probably meant the missing brackets in int p. All fixed now.
    – MarkB
    Apr 28, 2020 at 9:45
  • 1
    Hello Cliff and Monika. But wasn't this what she was doing when subtracting (25 * q) from change? int d = (change - (25 * q)) / 10; Oct 14, 2020 at 13:36
  • Good catch! I went back and looked more closely at it and the error is in the calculations of the pennies!
    – Cliff B
    Oct 14, 2020 at 18:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .