I’m trying to create a piece of code that would divide a set amount of money into their biggest bills constituents. Say, Input = 1200, then the code would print "[2]500 bills, [1]200 bills".
However using said Input the code would print "[2]500 bills, [2]100 bills". If I add a printf("%d",i); to see how the i values changes through the loop is does on a seemingly random manner; it also depends on the input given? Why?!
I can’t get my head around of why this is happening.
Code
#include <stdio.h>
#include <math.h>
int main()
{
float Input;
scanf("%f",&Input);
int SizeBilletes[7] = {500,200,100,50,20,10,5};
for(int i=0;i<7;i++){
if(Input/SizeBilletes[i]>1){
int a = floor(Input/SizeBilletes[i]);
Input -= a*SizeBilletes[i];
printf("[%d] en Billetes de %d\n",a,SizeBilletes[i]);
}
}
}
Important Notes:
- The Input has to be a float as the user might add a value that isn’t an integer (like cents or penys)
- If instead of a loop I use a ton of if statements to evaluate each size individually it works like a charm
- Using "Input %= SizeBill[i]" give me an error (was a solution given on a discord community)
- I’m very new to programming so apologies in advance if this is quite a simple problem.
>Solution :
Replace Input/SizeBilletes[i]>1 with Input / SizeBilletes[i] >= 1