Home > Enterprise >  Change Counting With Bills in C
Change Counting With Bills in C

Time:10-05

I am working on a program that takes in an amount owed and amount paid then calculates the amount of change needed to be given using fifty dollar bills, twenty dollar bills, etc. and also using quarters dimes and nickels.

This is the code:

#include <stdio.h>

int main(){
    double owed, paid;

    printf("Enter amount owed: ");
    scanf("%lf", &owed);

    printf("Enter amount paid: ");
    scanf("%lf", &paid);

    int owing = paid - owed;

    int fifty;
    if (owing > 0)
    {
        fifty = owing/50;
        printf("\n%d fifty dollar bill\n", fifty);
    }
    else
        printf("0 fifty dollar bill\n");
    owing = owing % 50;

    int twenty;
    if (owing > 0)
    {
        twenty = owing / 20;
        printf("%d twenty dollar bill\n", twenty);
    }
    else
        printf("0 twenty dollar bill\n");
    owing = owing % 20;


    int ten;
    if (owing > 0)
    {
        ten = owing / 10;
        printf("%d ten dollar bill\n", ten);
    }
    else
        printf("0 ten dollar bill\n");
    owing = owing % 10;

    int five;
    if (owing > 0)
    {
        five = owing / 5;
        printf("%d five dollar bill\n", five);
    }
    else
        printf("0 five dollar bill\n");
    owing = owing % 5;

    int toonie;
    if (owing > 0)
    {
        toonie = owing / 2;
        printf("%d two dollar coin\n", toonie);
    }
    else
        printf("0 two dollar coin\n");
    owing = owing % 2;

    int quarter;
    if (owing > 0)
    {
        quarter = owing / 25;
        printf("%d quarter\n", quarter);
    }
    else
        printf("0 quarter\n");
    owing = owing % 25;

    int dime;
    if (owing > 0)
    {
        dime = owing / 10;
        printf("%d dime\n", dime);
    }
    else
        printf("0 dime\n");
    owing = owing % 10;

    int nickel;
    if (owing > 0)
    {
        nickel = owing / 5;
        printf("%d nickel\n", nickel);
    }
    else
        printf("0 nickel\n");
    owing = owing % 5;

    

    
    return 0;
}

This is the current output

Enter amount owed: 16.50
Enter amount paid: 140.65

2 fifty dollar bill 
1 twenty dollar bill
0 ten dollar bill   
0 five dollar bill  
2 two dollar coin   
0 quarter
0 dime
0 nickel

This is the needed output:

Enter amount owed: 16.50
Enter amount paid: 140.65

2 fifty dollar bill 
1 twenty dollar bill  
2 two dollar coin   
1 dime
1 nickel

My issue is when the program gets to the cent calculation it doesn't give me back any cents. I have a feeling it has something to do with the fact that my "owing" is of type int. I have tried changing it to float but I don't know where to go after that. I am also a beginner to the "C" language and man is it frustrating... an early thankyou to whomever can give me a hand!

CodePudding user response:

update

I made a second variable in the beginning to store the cents in variablex = paid*100 - owed*100 and used that for my cent calculation by making the variable hold only the cents variablex = variablex % 100

CodePudding user response:

You can make your calculation in one function to get an better overview of your program. I did this with the calc function. Also I compiled the values in one array and their names in another array of the same size with the same order.

#include <stdio.h>

double dol_val[] = {50.0, 20.0, 10.0, 5.0, 2.0, 0.25, 0.10, 0.05};
const char * dol_name[] = {
    "fifty dollar bill",
    "twenty dollar bill",
    "ten dollar bill",
    "five dollar bill",
    "two dollar coin",
    "quarter",
    "dime",
    "nickel"
};
double calc(double owing_in, double bill, char *bill_name)
{
    double owing_out = owing_in;
    int bill_counter = 0;
    
    if(owing_out > 0)
    {
        bill_counter = (int)(owing_out / bill);
        if(bill_counter > 0)
        {
            printf("%d %s\n", bill_counter, bill_name);
            owing_out -= bill_counter * bill;
        }
    }
    
    return owing_out;    
}

int main(){
    double owed, paid;

    printf("Enter amount owed: ");
    scanf("%lf", &owed);

    printf("Enter amount paid: ");
    scanf("%lf", &paid);

    double owing = paid - owed;

    for(int i = 0; i < sizeof(dol_val); i  )
    {
        owing = calc(owing, dol_val[i], dol_name[i]);
    }
    
    return 0;
}

The functions gets called every loop cycle with the bill value and name from biggest to smallest. It will only print the bills that have a counter value > 0. And it will return the remaining owing value to the owing variable.

In the calc function I replaced the modulo operation. Modulo would be fine if you just using int variables. The whole function uses double values for the owing value. So you can make a simple division to get the number of bills that are in the remaining owing value, even for the cent values.

  • Related