I have been trying to help my friend with greedy without modulo (mine I did with modulo). We used the if conditions and for loops. The code works but counts coins strangely. When I give an input of 0.61 the result is 3, instead of 4. input 0.40 - result 2, when it should be 3.
I have changed the if conditions and for loops and put while loops instead. The program works perfectly. I just can't understand what is wrong with the first code. Why is machine skipping some if conditions?
Here's my code with if conditions:
#include<cs50.h>
#include<stdio.h>
#include<math.h>
int main(void)
{
printf ("Hi! How much is owed?\n");
int money;
float mon;
int count = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
do
{
mon = get_float();
}
while (mon < 0);
money = round(mon * 100);
// minus 25c
if (money >= 25)
{
for (int i = 0; money > 0; i++)
{
money = money - 25;
count1 = count1 + 1;
}
}
// minus 10c
if (money >= 10)
{
for (int i = 0; money > 0; i++)
{
money = money - 10;
count2 = count2 + 1;
}
}
// minus 5c
if (money >= 5)
{
for (int i = 0; money > 0; i++)
{
money = money - 5;
count3 = count3 + 1;
}
}
// for ones
if (money >= 1)
{
for (int i = 0; money > 0; i++)
{
money = money - 1;
count4 = count4 + 1;
}
}
count = count1 + count2 + count3 + count4;
printf ("%d\n", count);
return 0;
}