1

I'm taking a C programming class and I'm doing practice questions in the book. One of the questions is:

Write a program that asks the user to enter a U.S. dollar amount and then shows how to pay that amount using the smallest number of $20, $10, $5, %1 bills:

The sample is as follows:something right away that I may have overlooked when trying my code.

Enter a dollar amount: 93 (user inputted)

$20 bills: 4
$10 bills: 1
$5 bills: 0
$1 bills: 3

What I have so far is:

#include <stdio.h>

int main (void) 
{
 int cash;
 
 printf("Enter a dollar amount: ");
 scanf("%d", &cash);
     
 printf("$20 bills = %d\n", cash / 20);
 printf("$10 bills = %d\n", cash / 10);
 printf("$5 bills = %d\n", cash / 5);
 printf("$1 bills = %d\n", cash / 1);
 return 0;
}

The problem is, the book suggests dividing the input number (93) by 20, and since I'm using int instead of float, that leaves 4 instead of 4.65. Then it suggests subtracting the result of that times 20, from 93, leaving 13, and repeating that for each one. So it would be:

93/20=4
13/10=1
3/1=3

How can I get the printf("$10 bills = %d\n", cash / 10); line to recognize the value left by the previous line, printf("$20 bills = %d\n", cash / 20);? I had originally tried to put most of it on the 20 bill line, like cash / 20 * 20, but it just displayed 80 on that line instead of the next line.

7 Answers 7

9

I think you are mistaken a bit here. As you said, values are not left by the previous printf. You need to update the value of the variable cash. Here is what could help:

#include <stdio.h>

int main (void) 
{
    int cash;

    printf("Enter a dollar amount: ");
    scanf("%d", &cash);

    printf("$20 bills = %d\n", cash / 20);
    cash = cash % 20;
    printf("$10 bills = %d\n", cash / 10);
    cash = cash % 10;
    printf("$5 bills = %d\n", cash / 5);
    cash = cash % 5;
    printf("$1 bills = %d\n", cash);
    return 0;
}

The modulo (%) operator gives you back the remainder after performing the division. Hence:

93 % 20 = 13
13 % 10 = 3

and so on.

4
  • The module (%) operator gives you back the remainder after performing the division. Hence, 93 % 20 = 13, 13 % 10 = 3 and so on..
    – FooBar
    Sep 29, 2009 at 16:24
  • 1
    @FooBar, added that to the answer (where it belongs).
    – paxdiablo
    Oct 1, 2009 at 3:27
  • To pick a nit: if the amount entered is 7, you'll report $20 bills = 0, $10 bills = 0, ...? Oct 1, 2009 at 3:40
  • Of course, to counter my nitpicking, you can point to the '$5 bills: 0' in the question. I'm not sure it is all that elegant, but...we can also debate whether each amount should be on a line on its own, and various other details. Oct 1, 2009 at 3:45
5

cash is a variable meaning that it can vary in value. In other words, you can change the value of cash.

The % operator returns the remainder from a division. So if you do cash = 93 % 20; then cash should contain 13.

Remember that you can also set cash in relation to itself. So cash = cash % 20; would set cash to the value of the remainder of cash / 20.

4

Also, for the record, you may want to put the bill values in a table and iterate on this to avoid copy-pasted code:

int main()
{
    int billval[] = { 20, 10, 5, 1, 0 };
    int cash, i;

    printf("Enter a dollar amount: ");
    scanf("%d", &cash);

    for(i = 0; billval[i]; i++) {
        printf("$%d bills = %d\n", billval[i], cash / billval[i]);
        cash = cash % billval[i];
    }

    return 0;
}

Here, the billval table is zero-terminated so we know when to stop iterating<

2
  • So, if I enter 80, you will tell me 4 * $20, 0 * $10, 0 * $5, and 0 * $1? Shouldn't you be iterating on cash != 0 rather than billval[i] != 0? Oct 1, 2009 at 3:39
  • Of course, to counter my nitpicking, you can point to the '$5 bills: 0' in the question. Oct 1, 2009 at 3:44
1

You may have to use the modulo operator (%) which returns the remainder of the division of two numbers (for example, 93 % 20 equals 13).

For example :

int cash = 93;
int twenty, ten, five, one;

twenty = cash / 20;
cash = cash % 20;

ten = cash / 10;
cash = cash % 10;

/* etc. */
1
int main(){
int cash, bills_20, bills_10, bills_5, bills_1;

printf("Enter dollars amount: ");
scanf("%d",&cash);
bills_20=cash/20;
printf("$ 20 bills: %d\n",bills_20);
bills_10=(cash-bills_20*20)/10;
printf("$ 10 bills: %d\n",bills_10);
bills_5=(cash-bills_20*20-bills_10*10)/5;
printf("$ 5 bills: %d\n",bills_5);
bills_1=(cash-bills_20*20-bills_10*10-bills_5*5);
printf("$ 1 bills: %d\n",bills_1);
return 0;}
0

Thanks for all the help. Delroth & Imagist (well FooBar too, I dont know how I missed his comment while I was testing the theory since its exactly what I did) reminded me by simply adding another value for cash after each line would produce the result I was looking for.

#include <stdio.h>

int main (void) 
{
    int cash;

    printf("Enter a dollar amount: ");
    scanf("%d", &cash);

    printf("$20 bills = %d\n", cash / 20);
    cash = cash % 20;

    printf("$10 bills = %d\n", cash / 10);
    cash = cash % 10;

    printf("$5 bills = %d\n", cash / 5);
    cash = cash % 5;

    printf("$1 bills = %d\n", cash / 1);
    cash = cash % 1;
    return 0;
}

thats using the modulo % which hasnt been covered in the book yet, I'm going to try it again the way Pax suggested. Thanks again for the help

2
  • You don't need cash = cash % 1; on that last line; in fact, I don't think you ever need x = x % 1 since it should always return x. Sep 29, 2009 at 11:08
  • Well, you also don't need "cash / 1" in the printf but it adds a nice symmetry, don't you think :-) And there's an excellent chance both will be optimized away.
    – paxdiablo
    Sep 29, 2009 at 23:50
0

I would put another integer at the top to represent the number of bills for the current denomination:

int billCount;

then replace each section (except the $1 section) with:

billCount = cash / 20;                  // billCount <- 93/20 = 4.
printf("$20 bills = %d\n", billCount); 
cash -= (billCount * 20);               // cash <- cash - (4*20) = 13.

You can do it with modulo operators with less lines of code but the above code is what the suggestion from the book is pointing you toward.

Try to work it out using just the information above, if possible (it'll make you a better programmer). If you still can't do it, here's my full solution.

#include <stdio.h>

int main (void) {
    int cash, count;

    printf("Enter a dollar amount: ");
    scanf("%d", &cash);

    count = cash / 20;
    printf("$20 bills = %d\n", count);
    cash -= (count * 20);

    count = cash / 10;
    printf("$10 bills = %d\n", count);
    cash -= (count * 10);

    count = cash / 5;
    printf(" $5 bills = %d\n", count);
    cash -= (count * 5);

    printf(" $1 bills = %d\n", cash);

    return 0;
}
2
  • The code works, but it somehow feels wrong to teach a beginning programmer to rely on implicit rounding. Oct 1, 2009 at 3:59
  • Not at all in my opinion. The sooner they learn that, the quicker they'll stop asking why 3/2 doesn't give them 1.5. Rounding (truncating really) of integer types is a basic feature of the language.
    – paxdiablo
    Oct 1, 2009 at 4:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.