Reputation: 11
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.
Upvotes: 1
Views: 5079
Reputation: 11
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;}
Upvotes: 1
Reputation:
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.
Upvotes: 9
Reputation: 1208
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<
Upvotes: 4
Reputation: 881243
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;
}
Upvotes: 0
Reputation: 11
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
Upvotes: 0
Reputation: 18514
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
.
Upvotes: 5
Reputation: 10880
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. */
Upvotes: 1