I am doing cs50x and have run into a spot of trouble with my work. I am supposed to create an algorithm that will output the least amout of coins required to give back change. E.g 0.41 dollars will be 4 coins, a quarter (0.25), two dimes, (0.10) and a cent (0.01).For some reason this algorithm is not working (it is out putting the incorrect number of coins) and I cannot figure out why:
#include <stdio.h>
#include <cs50.h>
int Coins;
float Owed;
int main(void)
{
printf("How much is owed?\n");
Owed = GetFloat();
while (Owed < 0)
{
printf("A positive number please");
Owed = GetFloat();
}
if (Owed >= 0.25)
{
while (Owed >=0.25)
{
Owed = Owed - 0.25;
Coins++;
}
}
if (Owed >= 0.1)
{
while (Owed >=0.1)
{
Owed = Owed - 0.1;
Coins++;
}
}
if (Owed >= 0.05)
{
while (Owed >=0.05)
{
Owed = Owed - 0.05;
Coins++;
}
}
if (Owed >= 0.01)
{
while (Owed >= 0.01)
{
Owed = Owed - 0.01;
Coins++;
}
}
printf("%d",Coins);
}
When i ran the code and used 0.41 as the amount owed, i got 3 coins when the answer is supposed to be 4: