Exercise 3 --- Correct Change
When cashiers in a store give you change they try first try to "fit" dollars into the amount you get back, then try to "fit" quarters (25 cent coins) into what is left over, they try to "fit" dimes (10 cent coins) into what is now left over, then try to "fit" nickels (5 cent coins) into what is left, and finally are left with a few odd cents. For example, say that your change is 163 cents:
One dollar fits into 163, leaving 63 cents.
Two quarters fit into 63 cents, leaving 13 cents.
One dime fits into 13 cents, leaving 3 cents.
No nickels are needed.
Three cents are left.Your change is : 1 dollar, two quarters, one dime, and three cents.
Write a program that reads change due to a user (in cents) and writes out how many dollars, quarters, dimes, nickels, and pennies she is due. If you are stuck, it will help to do an example problem with paper and pencil.
http://stackoverflow.com/questions/33250065/convert-cents-to-quarters-nickels-dimes-and-pennies-using-python
I took the codes from here and modified it slightly, but the program crashes whenever I try to run it. What's wrong?
Code:
#include<stdio.h>
int main() // Convert some money to an appropriate collection of cents
{
int cent = 1;
int nickel = 5; // Each nickel consists of 5 cents.
int dime = 10; // Each dime consists of 10 cents.
int quarter = 25; // Each quarter consists of 25 cents.
int dollar = 100; // Each dollar consists of 100 cents.
int dollars = 0;
int quarters = 0;
int dimes = 0;
int nickels = 0;
int cents = 0;
printf("Please enter an amount of money you have in cents: ");
scanf("%d",¢s); //Total cents input by the user
do{
if (cents >= 100)
{
dollars = cents / dollars; // How many dollars we can get?
cents = cents % dollars;
}
else if (cents >= 25)
{
quarters = cents / quarter; // How many quarters we can get?
cents = cents % quarter;
}
else if (cents >= 10)
{
dimes = cents /dime; // How many dimes we can get?
cents = cents % dime;
}
else if (cents >= 5)
{
nickels = cents /nickel; // How many nickels we can get?
cents = cents % nickel;
}
else
{
cents = cent; // How many cents we'd have left?
}
}while (cents != 0);
printf("Your change is : %d dollar, %d quarters, %d dime, %d nickels, and %d cents.\n", dollars, quarters, dimes, nickels, cents);
return 0;
}