Reputation: 21
The code is apparently working, but when inserting the value 576.73
, the code fails to return the value in cents.
This is beecrowd exercise 1021.
#include <stdio.h>
#include <math.h>
int calculo(float num);
int main() {
float numero;
scanf("%f",&numero);
calculo(numero);
return 0;
}
int calculo(float num) {
//TRANSFORMA PARA INTEIRO
int n;
//NOTAS
int n100, n50, n20, n10, n5, n2;
//MOEDAS
int m1, m05, m025, m010, m005, m001;
//NOTAS
n = floor(num);
n100 = n/100;
n50 = (n%100)/50;
n20 = ((n%100)%50)/20;
n10 = (((n%100)%50)%20)/10;
n5 = ((((n%100)%50)%20)%10)/5;
n2 = (((((n%100)%50)%20)%10)%5)/2;
//MOEDAS
m1 = (((((n%100)%50)%20)%10)%5)%2;
n = num*100;
n = (int) n*1;
n = n%100;
m05 = n/50;
n = n%50;
m025 = n/25;
n = n%25;
m010 = n/10;
n = n%10;
m005 = n/5;
m001 = n%5;
printf("NOTAS:\n");
printf("%d nota(s) de R$ 100,00\n",n100);
printf("%d nota(s) de R$ 50,00\n",n50);
printf("%d nota(s) de R$ 20,00\n",n20);
printf("%d nota(s) de R$ 10,00\n",n10);
printf("%d nota(s) de R$ 5,00\n",n5);
printf("%d nota(s) de R$ 2,00\n",n2);
printf("MOEDAS:\n");
printf("%d moeda(s) de R$ 1,00\n",m1);
printf("%d moeda(s) de R$ 0,50\n",m05);
printf("%d moeda(s) de R$ 0,25\n",m025);
printf("%d moeda(s) de R$ 0,10\n",m010);
printf("%d moeda(s) de R$ 0,05\n",m005);
printf("%d moeda(s) de R$ 0,01\n",m001);
return 0;
}
Upvotes: 0
Views: 226
Reputation: 21
#include <stdio.h>
int calculo(double num);
int main() {
double numero;
scanf("%lf",&numero);
calculo(numero);
return 0;
}
int calculo(double num) {
//TRANSFORMA PARA INTEIRO
int n;
//NOTAS
int n100, n50, n20, n10, n5, n2;
//MOEDAS
int m1, m05, m025, m010, m005, m001;
//NOTAS
n = num;
n100 = n/100;
n50 = (n%100)/50;
n20 = ((n%100)%50)/20;
n10 = (((n%100)%50)%20)/10;
n5 = ((((n%100)%50)%20)%10)/5;
n2 = (((((n%100)%50)%20)%10)%5)/2;
//MOEDAS
m1 = (((((n%100)%50)%20)%10)%5)%2;
n = num*100;
n = n%100;
m05 = n/50;
n = n%50;
m025 = n/25;
n = n%25;
m010 = n/10;
n = n%10;
m005 = n/5;
m001 = n%5;
printf("NOTAS:\n");
printf("%d nota(s) de R$ 100,00\n",n100);
printf("%d nota(s) de R$ 50,00\n",n50);
printf("%d nota(s) de R$ 20,00\n",n20);
printf("%d nota(s) de R$ 10,00\n",n10);
printf("%d nota(s) de R$ 5,00\n",n5);
printf("%d nota(s) de R$ 2,00\n",n2);
printf("MOEDAS:\n");
printf("%d moeda(s) de R$ 1,00\n",m1);
printf("%d moeda(s) de R$ 0,50\n",m05);
printf("%d moeda(s) de R$ 0,25\n",m025);
printf("%d moeda(s) de R$ 0,10\n",m010);
printf("%d moeda(s) de R$ 0,05\n",m005);
printf("%d moeda(s) de R$ 0,01\n",m001);
return 0;
}
Upvotes: 0
Reputation: 4816
In reviewing your question and requirement for currency and coin, since the entered monetary value will be in a format with two digits to the right of the decimal place, utilizing integer values would still be feasible as noted in the good comment above. What would need to be done is to view the entered amount in the smallest value of money for your currency which appears to be Euros.
With that in mind, following is a refactored version of the code allowing the user to enter in a monetary value with a decimal place, but internally convert the entered value into an integer value and then evaluate that amount into the equivalent number of notes and coins.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int calculo(int num);
int main()
{
char inp[10];
int numero = 0, counter, multiplier = 1;
printf("Enter monetary amount: "); /* User friendly prompt */
if (scanf("%s",inp) != 1) /* Input a string */
{
printf("Input error\n");
return -1;
}
counter = strlen(inp);
while (1) /* Read through the string and convert to an integer value */
{
if (inp[counter] >= '0' && inp[counter] <= '9')
{
numero += (inp[counter] - '0') * multiplier;
multiplier *= 10;
}
counter --;
if (counter < 0)
{
break;
}
}
calculo(numero); /* Calculate the quantity of currency and coins */
return 0;
}
int calculo(int num)
{
//TRANSFORMA PARA INTEIRO
int n;
//NOTAS
int n100, n50, n20, n10, n5, n2;
//MOEDAS
int m1, m05, m025, m010, m005, m001;
//NOTAS
n = num / 100; /* In lieu of using the math "floor" function */
n100 = n/100;
n50 = (n%100)/50;
n20 = ((n%100)%50)/20;
n10 = (((n%100)%50)%20)/10;
n5 = ((((n%100)%50)%20)%10)/5;
n2 = (((((n%100)%50)%20)%10)%5)/2;
//MOEDAS
m1 = (((((n%100)%50)%20)%10)%5)%2;
n = num - n * 100; /* Acquire the value for calculating change */
//n = (int) n*1;
n = n%100;
m05 = n/50;
n = n%50;
m025 = n/25;
n = n%25;
m010 = n/10;
n = n%10;
m005 = n/5;
m001 = n%5;
printf("NOTAS:\n");
printf("%d nota(s) de R$ 100,00\n",n100);
printf("%d nota(s) de R$ 50,00\n",n50);
printf("%d nota(s) de R$ 20,00\n",n20);
printf("%d nota(s) de R$ 10,00\n",n10);
printf("%d nota(s) de R$ 5,00\n",n5);
printf("%d nota(s) de R$ 2,00\n",n2);
printf("MOEDAS:\n");
printf("%d moeda(s) de R$ 1,00\n",m1);
printf("%d moeda(s) de R$ 0,50\n",m05);
printf("%d moeda(s) de R$ 0,25\n",m025);
printf("%d moeda(s) de R$ 0,10\n",m010);
printf("%d moeda(s) de R$ 0,05\n",m005);
printf("%d moeda(s) de R$ 0,01\n",m001);
return 0;
}
In the U.S. we would indicate that we are working with an equivalent value of "cents" as opposed to "dollars", so values being evaluated are either multiplied by "100" or divided by "100" depending upon the circumstance. Following are some key points to the refactored code.
With those noted changes, following is the test input and output utilizing the value originally noted in this question.
@Vera:~/C_Programs/Console/Money/bin/Release$ ./Money
Enter monetary amount: 576.73
NOTAS:
5 nota(s) de R$ 100,00
1 nota(s) de R$ 50,00
1 nota(s) de R$ 20,00
0 nota(s) de R$ 10,00
1 nota(s) de R$ 5,00
0 nota(s) de R$ 2,00
MOEDAS:
1 moeda(s) de R$ 1,00
1 moeda(s) de R$ 0,50
0 moeda(s) de R$ 0,25
2 moeda(s) de R$ 0,10
0 moeda(s) de R$ 0,05
3 moeda(s) de R$ 0,01
A quick inspection of the note quantities and coin quantities appear to be correct.
Again, to expand upon the comment above, handling monetary amounts is usually performed with integer variables to avoid possible floating point variable issues.
Give this refactored code a try and see if it meets the spirit of your project.
Upvotes: 0