Thread: C Program to convert a dollar amount

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    1

    C Program to convert a dollar amount

    The program is supposed to convert a entered dollar amount into dollars, half dollars, quarters, dimes, nickels, and pennies. I have some of the program but I can't figure out the exact math. PLEASE HELP!!!!

    Here's code I have:
    insert
    Code:
    include <stdio.h>
    #include <math.h>
    
    
    void convert(float amount);
    
    
    void main(){
        //Do not change this function
        float amount;
    
    
        printf("Enter the amount: $");
        scanf("%f", &amount);
        convert(amount);
    }
    
    
    void convert(float amount){
    
    
        float dollar, hdollar,quarter, dime, nickel, penny;
        double hd = 0.50, q = 0.25, d = 0.10, n = 0.05, p = 0.01; 
    
    
        dollar = amount / 1.00;
        hdollar = (amount - (dollar * 1)); //% 50;
        quarter = (amount - ((dollar * 1) - (hdollar * 50))); // % 25;
        dime = (amount - ((dollar * 1) - (hdollar * 50) - (quarter * 25))); // % 10;
        nickel = (amount - ((dollar * 1) - (hdollar * 50) - (quarter * 25) - (dime * 10))); // % 5;
        penny = (amount - ((dollar * 1) - (hdollar * 50) - (quarter * 25) - (dime * 10) - (nickel * 5))); // % 1;
         
    
    
        printf ("Dollar Bills: %lf\n", dollar);
        printf ("hdollar: %lf\n", hdollar);
        printf ("Half Dollars: %lf\n", fmod(hdollar,hd));
        printf ("quarters: %lf\n", fmod(quarter,q));
        printf ("Dimes: %lf\n", fmod(dime,d));
        printf ("Nickle: %lf\n", fmod(nickel, n));
        printf ("Pennies: %lf\n", fmod(penny,p));
    
    
        
    }
    Here's an example of what it's supposed to do:
    Enter the amount: $7.73
    Dollar bills: 7
    Half dollars: 1
    Quarters: 0
    Dimes: 2
    Nickels: 0
    Pennies: 3

  2. #2
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Several ways to do it. I wouldn't overuse floating point maths. I much prefer integer maths for something like this. Amount has to be floating point, but do doubles seem sensible for number of dollars, half dollars etc? What does 0.3 of a half dollar look like?

    Send in say 7.73 to the convert function.
    Step one convert that to a number of cents? ( *100 )
    773 cents.
    dollars is simple number of cents / 100 which in integer division returns 7.
    subtract number of dollars * 100 from number of cents.
    73 cents left.
    half dollars is number of cents / 50 which returns 1 in integer division.
    subtract number of half dollars * 50 from number of cents
    23 cents left.
    And so on.

    That's one way to do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting change to dollar amount in C
    By Staja24 in forum C Programming
    Replies: 2
    Last Post: 04-18-2015, 08:32 PM
  2. Trying to convert a total amount into change
    By American Raptor in forum C Programming
    Replies: 9
    Last Post: 12-07-2008, 10:33 PM
  3. Incrementing a Dollar amount
    By vamshiy in forum C Programming
    Replies: 5
    Last Post: 06-01-2008, 04:38 PM
  4. Convert an amount to a string
    By g_p in forum C Programming
    Replies: 20
    Last Post: 12-12-2006, 05:15 AM
  5. Replies: 2
    Last Post: 03-15-2006, 08:14 AM

Tags for this Thread