1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Rep Power

    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.


    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. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Rep Power
    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!

IMN logo majestic logo threadwatch logo seochat tools logo