Problem banknotes and coins

Asked

Viewed 112 times

1

The given question required that scanf be used to read a certain floating-point value from 0 to 1000000 and print how many 100, 50, 20, 10, 5 Reals and 2 Reals, and how many 1 Reals, 50.25, 10.5.1 Centavos would have entered value. The code is reading and printing the values correctly, but the platform does not accept the code.

 #include <stdio.h>
 #include <stdlib.h>
int main (){
    int valor1,valor2;
    int count_100=0,count_50=0,count_20=0,count_10=0,count_5=0,count_2=0;
    int count_50cents=0,count_1=0,count_25cents=0,count_10cents=0,count_5cents=0,count_1cents=0;
scanf("%f",&valor);
while(valor>=100){
    valor=valor-100;
    count_100++;
}
while(valor>=50){
    valor=valor-50;
    count_50++;
}
while(valor>=20){
    valor=valor-20;
    count_20++;
}
while(valor>=10){
    valor=valor-10;
    count_10++;
}
while(valor>=5){
    valor=valor-5;
    count_5++;
}
while(valor>=2){
    valor=valor-2;
    count_2++;
}
while(valor>=1){
    valor=valor-1;
    count_1++;
}
while(valor>=0.50){
    valor=valor-0.50;
    count_50cents++;
}
while(valor>=0.25){
    valor=valor-0.25;
    count_25cents++;
}
while(valor>=0.10){
    valor=valor-0.10;
    count_10cents++;
}
while(valor>=0.05){
    valor=valor-0.05;
    count_5cents++;
}
while(valor>0){
    valor=valor-0.01;
    count_1cents++;
}
printf("NOTAS:\n");
printf("%d nota(s) de R$ 100.00\n",count_100);
printf("%d nota(s) de R$ 50.00\n",count_50);
printf("%d nota(s) de R$ 20.00\n",count_20);
printf("%d nota(s) de R$ 10.00\n",count_10);
printf("%d nota(s) de R$ 5.00\n",count_5);
printf("%d nota(s) de R$ 2.00\n",count_2);
printf("MOEDAS:\n");
printf("%d moeda(s) de R$ 1.00\n",count_1);
printf("%d moeda(s) de R$ 0.50\n",count_50cents);
printf("%d moeda(s) de R$ 0.25\n",count_25cents);
printf("%d moeda(s) de R$ 0.10\n",count_10cents);
printf("%d moeda(s) de R$ 0.05\n",count_5cents);
printf("%d moeda(s) de R$ 0.01\n",count_1cents);
return 0;
}
  • Young man, where are you setting the variable value? Note that you try to read it in the scanf("%f",&value) snippet; but it does not have this variable. Try to declare and see if it solves, let’s talk :)

2 answers

-1


You can use a variable of type double to store a floating point value and read it with scanf() using the format specifier %lf, look at you:

double valor;
scanf("%lf",&valor);

Finally, your last loop while(), responsible for the counting of 1 centavo, is incorrect and should be something like:

while( valor >= 0.01 ){
    valor=valor-0.01;
    count_1cents++;
}

In this way, the algorithm will consider entries containing values up to the "home" of the pennies, always rounding to "down" if there is some fraction of a penny in the input typed, for example:

123.123
NOTAS:
1 nota(s) de R$ 100.00
0 nota(s) de R$ 50.00
1 nota(s) de R$ 20.00
0 nota(s) de R$ 10.00
0 nota(s) de R$ 5.00
1 nota(s) de R$ 2.00
MOEDAS:
1 moeda(s) de R$ 1.00
0 moeda(s) de R$ 0.50
0 moeda(s) de R$ 0.25
1 moeda(s) de R$ 0.10
0 moeda(s) de R$ 0.05
2 moeda(s) de R$ 0.01

Rounded to 123.12, ignored 0.003.

Or:

999.999
NOTAS:
9 nota(s) de R$ 100.00
1 nota(s) de R$ 50.00
2 nota(s) de R$ 20.00
0 nota(s) de R$ 10.00
1 nota(s) de R$ 5.00
2 nota(s) de R$ 2.00
MOEDAS:
0 moeda(s) de R$ 1.00
1 moeda(s) de R$ 0.50
1 moeda(s) de R$ 0.25
2 moeda(s) de R$ 0.10
0 moeda(s) de R$ 0.05
4 moeda(s) de R$ 0.01

Rounded to 999.99, ignored 0.009.

-2

I found some errors in your code, tidied and worked perfectly, first you are declaring integer values instead of floating, in the scanf you are asking for the value of the variable "value", but you did not declare this variable and yes "value 1 and value 2", follows below the tidy code, tested the same here and worked.

int main (){
float valor;
//o mesmo vale para outras variaveis(altere de int para float.)
scanf("%f",&valor);
//para valores flutuantes utiliza-se %f para receber os valores.

down there also change printfs to (%.0f), so you will be declaring the floating values and saying that you do not want any house in decimal.

rest this same.

Browser other questions tagged

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