Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
July 3rd, 2012, 03:59 AM
 susan12
Registered User

Join Date: Jul 2012
Posts: 1
Time spent in forums: 47 m 12 sec
Reputation Power: 0
Beginner C Programming Question

In my introduction to computer programming course i was a given a project asking to create a change maker. Specifically, i must write a program that will make change in coins for any amount up to 99 cents using the fewest possible coins. The program should prompt the user to enter an amount, and then print out the number of each type of coin to make that amount of change. If there are no coins of a particular type, then the program should not print a line for that coin.

Examples:

Enter the change amount: 93
Quarters: 3
Dimes: 1
Nickels: 1
Pennies: 3

Enter the change amount: 89
Quarters: 3
Dimes: 1
Pennies: 4

This is what i have so far. The program works unless the "change" value falls under 25 so i'm assuming the problem involves the "if" statements. I have been stuck for a while and was hoping a misc brah could help me out with some suggestions.

#include <stdio.h>
main ()
{
int change;
int coins;
int remainder;

printf ("Please enter an amount of change from 0-99.\n");
scanf ("%d", &change);

if (change>=25)
{
coins = change/25;
printf ("Quarters: %d\n", coins);
remainder = change%25;
}

if (remainder>=10)
{
coins = remainder/10;
printf ("Dimes: %d\n", coins);
remainder = remainder%10;
}

if (remainder>=5)
{
coins = remainder/5;
printf ("Nickles: %d\n", coins);
remainder = remainder%5;
}

if (remainder>0)
{
printf ("Pennies: %d\n", remainder);
}

}

#2
July 3rd, 2012, 05:57 AM
 ptr2void
I haz teh codez!

Join Date: Dec 2003
Posts: 2,511
Time spent in forums: 1 Month 2 Weeks 3 Days 11 m 12 sec
Reputation Power: 2274
Well, just put a little thought into it. If the value of change starts out less than 25, are which of your if statements are going to be executed?
__________________
I ♥ ManiacDan & requinix

This is a sig, and not necessarily a comment on the OP:
Please don't be a help vampire!

 Viewing: Dev Shed Forums > Other > Beginner Programming > Beginner C Programming Question