Search code examples
ccalculatorcurrency

C Program to find the number of denominations for a given amount


now I'am nearly finished with my program but I need a little help because the math is not right.

Since I'am linking to menge with euro i thought the script will use 414. But it's somehow using an other number.

I'am still a little bit confused with xyz.c files and header.h files. Should I put data entries into the .h file and functions into .c file. How should I use them correctly.

int main()
{
    double euro, ats, sum;
    // Eingabe euro
    printf("Input: ");
    scanf("%lf", &euro);

    ats = 13.760;  //fixierter Wert für die Umrechnung
    // Umrechung Euro mal ats
    sum = euro * ats;
    printf("Ausgabe:\n");
    printf("%.0f Euro entspricht ca. %.0f Schilling\n", euro, sum);

    //Textausgabe mit Geldnoten
    int menge, geldnoten;
    menge = euro;
    // Stueckelung Noten 
    int stueckelung[SIZE] = {200,100,50,20,10,5,2,1};

    for (int i = 0; i < SIZE; i++)
    {
        geldnoten = menge / stueckelung[i];

        if(geldnoten)
        {
            geldnoten = menge % stueckelung[i]; //uebriges Geld

            printf("%d + %d = %d \n", geldnoten, stueckelung[i],
                geldnoten * stueckelung[i]);
        }        
    }

    return 0;
}

Output:

Input: 414
Ausgabe:
414 Euro entspricht ca. 5697 Schilling
14 + 200 = 2800 
14 + 100 = 1400 
14 + 50 = 700 
14 + 20 = 280 
4 + 10 = 40 
4 + 5 = 20 
0 + 2 = 0 
0 + 1 = 0 

The goal of the output is this sentence: In Geldscheinen und Münzen: 2 Scheine mit Wert 200, 1 Schien mit Wert 10 and 2 Münzen mit Wert 2

The translation would be: In banknotes and coins: 2 notes 200, 1 note of 10 and 2 coins of 2.

I'am struggling here with the printf command how should I do this?


Solution

  • If I understand the problem correctly, you need to reduce menge with geldnoten * stueckelung[i] (the product of the number of notes , geldnoten, and the value of each note, stueckelung[i]) in each iteration of the loop:

        for (int i = 0; i < SIZE; i++)
        {
            geldnoten = menge / stueckelung[i];
    
            if(geldnoten)
            {
                // geldnoten = menge % stueckelung[i]; // not this
                menge -= geldnoten * stueckelung[i];   // this instead
    
                printf("%d * %d = %d\n", geldnoten, stueckelung[i],
                    geldnoten * stueckelung[i]);
            }
        }
    

    With the input 414, the above would produce this output:

    Input: 414
    Ausgabe:
    414 Euro entspricht ca. 5697 Schilling
    2 * 200 = 400
    1 * 10 = 10
    2 * 2 = 4