In: Computer Science
Hints: use if / else if / else... use % and /... use a while loop or a do while loop, no arrays
example:
Please enter the number of cents, from 1 through 99,
or enter a negative number to quit: 31
Quarters: 1 Dimes: 0 Nickels: 1
Pennies: 1
Please
enter the number of cents, from 1 through 99, or enter a negative
number to quit: 89
Quarters: 3
Dimes: 1 Nickels: 0 Pennies: 4
Please
enter the number of cents, from 1 through 99, or enter a negative
number to quit: 121
Your entered a value outside the
range.
Please enter the number of cents, from 1 through 99, or enter a
negative number to quit: 0
Your entered a value outside the range.
Please
enter the number of cents, from 1 through 99, or enter a negative
number to
quit: 18
Quarters: 0 Dimes: 1 Nickels: 1 Pennies: 3
Your entered a value outside the
range.
Please enter the number of cents, from 1 through 99, or enter a
negative number to
quit: -666
Thanks for using my program. Good
bye.
program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int amount; /* how much
change the user has */
int leftover; /* used to hold amount as it
decreases */
int numquarters; /* how many quarters */
int numdimes; /* how many dimes */
int numnickels; /* how many nickels */
int numpennies; /* how many pennies */
/* read in the amount and save it */
printf("Please enter the number of cents, from 1
through 99, or enter a negative number to quit:");
scanf("%d", &amount);
while(amount>=0)
{
if(amount == 0 ||
amount>99)
{
printf("Your
entered a value outside the range.\n");
}
else
{
leftover =
amount;
/* computer the
number of quarters and how much is left over */
numquarters =
leftover / 25;
leftover =
leftover % 25;
/* computer the
number of dimes and how much is left over */
numdimes =
leftover / 10;
leftover =
leftover % 10;
/* computer the
number of nickels and how much is left over */
numnickels =
leftover / 5;
leftover =
leftover % 5;
/* whatever is
left over is the number of pennies */
numpennies =
leftover;
/* print the
result */
printf("Quarters: %d Dimes: %d Nickels: %d Pennies: %d\n",
numquarters, numdimes, numnickels, numpennies);
}
printf("Please enter the number of
cents, from 1 through 99, or enter a negative number to
quit:");
scanf("%d", &amount);
}
printf("Thanks for using my program. Good
bye.");
}
output: