Problem notes and coins [closed]

1
The given question required that you use scanf to read a floating-point value from 0 to 1000000 and print out how many 100, 50, 20, 10, 5, and 2 real, and how many 1-real, 50, 25,10,5,1 cent would have the value inserted. 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;
}
    
asked by anonymous 14.10.2018 / 00:17

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 , see:

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

Finally, your last loop while() , which is responsible for counting the coins 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 "house" of the cents, always rounding to "low" if there is a fraction of a cent in the input entered, 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 .

    
15.10.2018 / 19:59
-2

I found some errors in your code, I did it and it 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 but "value 1 and value 2 ", here is the code below, I tested it here and it 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.

Below also change the printfs to (% .0f), so you are declaring the floating values and saying that you do not want any houses in the decimal.

rest is equal.

    
14.10.2018 / 20:34