greedy algorithm (with coins) in C
<p>In my code I usede the greedy algorithm in order to use the minimum amaount of coins. For example: I must return $0.41, the minimum amount of coins I can use is 4:</p>

<pre><code>1 - 0,25;
1 - 0.10;
1 - 0.05;
1 - 0.01;
</code></pre>

<p>There are 4 types of coins: 0.25,0.10,0.05,0.01.</p>

<p>Here is my code:</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;cs50.h&gt;

int main(void)
{
    printf("Enter the sum, that you want to return you:");
    float sum = GetFloat();
    float quaters = 0.25;
    float dime = 0.10;
    float nickel = 0.05;
    float penny = 0.01;
    int count_q = 0,count_d = 0,count_n = 0,count_p = 0;


    while(sum&lt;0){
        printf("Incorrect, enter the positive float number");
        sum = GetFloat();
    }

    while(sum &gt; 0){
        if(sum - quaters &gt;=0){
            sum -=quaters;
            count_q +=1;
        }
        else if((sum -quaters &lt;0) &amp;&amp; (sum -dime&gt;=0)){
            sum -= dime;
            count_d +=1;
        }
        else if((sum - dime &lt;0) &amp;&amp;(sum - nickel&gt;=0) ){
            sum -= nickel;
            count_n +=1;
        }
        else if(sum - nickel &lt;0){
            sum -= penny;
            count_p +=1;
        }
    }
    printf("The number of quaters: %i\n",count_q);
    printf("The number of dimes: %i\n",count_d);
    printf("The number of nickels: %i\n",count_n);
    printf("The number of pennies: %i\n",count_p);
}
</code></pre>

<p>This code calculates how many coins of each type of was used to return the sum. In most cases it works fine.</p>

<p>But sometimes, for example, when i enter the number 1.12 it gives me wrong result:</p>

<pre><code>Enter the sum, that you want to return you:1.12
The number of quaters: 4
The number of dimes: 1
The number of nickels: 0
The number of pennies: 3
</code></pre>

<p>I think, that the problem is in last else if statement. But i don't know how can I correct it.</p>