how to make a program in which a monetary value is entered and displayed on the screen the amount of banknotes and coins of each type will be used

Asked

Viewed 44 times

0

Hello, I’m trying to perform a program in which, you have to ask the user to enter a monetary value( random ) and let be displayed on the screen the amount of banknotes and coins of each type together total this value. However, each banknote has a quantity restriction. For example: 4 banknotes of 200.00. 2 of 100,00

I’ve been trying for days, but I can’t figure out what’s going wrong, it’s getting huge, and besides, I believe I’m putting something out of order. that’s my code for now:

#include <stdio.h>

int main()
{
    float centavos;
    int valor, ced200, ced100, ced50, ced20, ced10, ced5, ced2, ced1, moe050, moe025, moe010, moe005, moe001;
    ced200 = 0;
    ced100 = 0;
    ced50 = 0;
    ced20 = 0;
    ced10 = 0;
    ced5 = 0;
    ced2 = 0;
    ced1 = 0;
    moe050 = 0;
    moe025 = 0;
    moe010 = 0;
    moe005 = 0;
    moe001 = 0;



    printf(" Digite um valor monetario:");
    scanf( "%d" , &valor );
    printf(" Digite um valor para moedas:");
    scanf( "%f" , &centavos );

    while( valor >= 200 );
    {
         ced200 = +1;
         valor = valor - 200;
    }
    while( valor >= 100 )
    {
         ced100 = +1;
         valor = valor - 100;
    }
    while( valor >= 50 )
    {
         ced50 = +1;
         valor = valor - 50;
    }
    while( valor >= 20 )
    {
         ced20 = +1;
         valor = valor - 20;
    }
    while( valor >= 10 )
    {
         ced10 = +1;
         valor = valor - 10;
    }
    while( valor >= 5 )
    {
         ced5 = +1;
         valor = valor - 5;
    }
    while( valor >= 2 )
    {
         ced2 = +1;
         valor = valor - 2;
    }
    while( valor >= 1 )
    {
         ced1 = +1;
         valor = valor - 1;
    }
    while( centavos >= 0.50 )
    {
         moe050 = +1;
         centavos = centavos - 0.50;
    }
    while( centavos >= 0.25 )
    {
         moe025 = +1;
         centavos = centavos - 0.25;
    }
    while( centavos >= 0.10 )
    {
         moe010 = +1;
         centavos = centavos - 0.10;
    }
    while( centavos >= 0.05 )
    {
         moe005 = +1;
         centavos = centavos - 0.05;
    }
    while( centavos >= 0.01 )
    {
         moe001 = +1;
         centavos = centavos - 0.01;
    }
    
    if ( ced200 > 4)
    {
        printf(" não existe notas suficientes");
    }
    else if ( ced100 > 2)
    {
        printf(" não existe notas suficientes");
    }
    else if ( ced50 > 3)
    {
        printf(" não existe notas suficientes");
    }
    else if ( ced20 > 3)
    {
        printf(" não existe notas suficientes");
    }
    else if ( ced10 > 4)
    {
        printf(" não existe notas suficientes");
    }
    else if ( ced5 > 2)
    {
        printf(" não existe notas suficientes");
    }
    else if ( ced2 > 3)
    {
        printf(" não existe notas suficientes");
    }
    else if ( ced1 > 5)
    {
        printf(" não existe notas suficientes");
    }
    else if ( moe050 > 6)
    {
        printf(" não existe moedas suficientes");
    }
    else if ( moe025 > 4)
    {
        printf(" não existe moedas suficientes");
    }
    else if ( moe010 > 3)
    {
        printf(" não existe moedas suficientes");
    }
    else if ( moe005 > 8)
    {
        printf(" não existe moedas suficientes");
    }
    else if ( moe001 > 7)
    {
        printf(" não existe moedas suficientes");
    }

    else
    {
        printf(" Nao e possivel formar esse valor com as cedulas e moedas disponiveis ");
    }
    printf("A quantidade de notas de 200 é:",ced200,"ced100 é",ced100,"ced50 é",ced50,"ced20 é",ced20,"ced10 é",ced10,"ced5 é",ced5,"ced2 é",ced2,"ced1 é",ced1,"moe050",moe050,"moe025",moe025,"moe010",moe010,"moe005",moe005,"moe001",moe001);


    return 0;
}

Where can I be missing?

  • Have you ever thought about using arrays? Surely your code will become much more compact.

  • Assess the operator’s use % (rest of the division) you will eliminate this lot of whiles. Note that you have declared its value as integer and therefore its value cannot contain the decimal part (the cents). In fact in C the decimal separator is . and not ,. Maybe it will be easier for the calculations you multiply the values by 100 and work only with integers. Still valid the previous comment.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.