Thread: Change Program...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    11

    Change Program...

    So I'm making a change program! Exciting!
    I've only learned how to use loops... And if else statements.
    So I used a lot of if/else statements in my code and was hoping there's a more efficient way to do this. I'm supposed to print out the change breakdown of the given amount. like so...
    34.67 entered
    1 20.00
    1 10.00
    0 5.00
    4 1.00
    2 .25
    1 .10
    1 .05
    2 .01

    I just am trying to understand loops better without having to use so many if/else statements. I don't want to get stuck into a habit of just using that all the time... Here's my code...
    Code:
    #include <stdio.h>
    
    int main (void)
    {
        //Declare Variables
        
        int twenty,ten,five,one, dime, nickle, penny;
        double dollar_amt, cents;
                   
        //Have User Enter An Amount    
            printf("Please enter an amount: ");
            scanf("%lf", &dollar_amt);
        
        //Check if user has correct amount entered, if not have them re-enter it
        while (dollar_amt > 100.00 || dollar_amt < 0)
        {    
            
            printf("Please input an amount no more than 100.00: ");
            scanf("%lf", &dollar_amt);
                
        }
        
        //Print the amount entered
        printf("The amount entered is %2.2lf\n", dollar_amt);
        
        twenty = 20;
        if (dollar_amt > 20)
        {
            (int)dollar_amt % twenty;
        }
        else
        {
            printf("\n");
        }
        
        ten = 10;
        if (dollar_amt > 10)
        {
            (int)dollar_amt % ten;
        }
        else
        {
            printf("\n");
        }
    
        five = 5;
        if (dollar_amt > 5)
        {
            (int)dollar_amt % five;
        }
        else
        {
            printf("\n");
        }
        one = 1;
        if (dollar_amt > 1)
        {
            (int)dollar_amt % 1;
        }
    
        printf("\n");
    
    //End Program
    return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Does your program output the result you want? Because I can see that it doesn't... Fix that first and then try for a more efficient way.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dispense Change program
    By paranoidgnu in forum C Programming
    Replies: 9
    Last Post: 04-18-2011, 01:07 PM
  2. C program to Calculate Change
    By CaptMorgan in forum C Programming
    Replies: 8
    Last Post: 04-07-2010, 12:31 AM
  3. help with change program
    By f4ichick02 in forum C Programming
    Replies: 12
    Last Post: 10-21-2009, 02:49 PM
  4. Weight Change Program - Help
    By battousaih in forum C Programming
    Replies: 14
    Last Post: 10-01-2007, 04:34 PM
  5. coin change program??
    By kelly in forum C++ Programming
    Replies: 3
    Last Post: 03-24-2002, 08:57 PM