Pset1- GREEDY PROBLEM

while((a!=0)&&(a>0))
{
printf("LOOP ENTERED\n");
if(a>=0.25)
{
a=a-0.25;
i++;
}

else if((a<0.25)&&(a>=0.10))
{
a=a-0.10;
i++;
}
else if((a<0.10)&&(a>=0.05))
{
a=a-0.05;
i++;
}
else if((a<0.05)&&(a>=0.01))
{
a=a-0.01;
i++;
}
}

why is this an infinite loop? IS this approach wrong. I have initialized i=0 at start. And a is the amount in dollars.

• Additional to @Cliff B's answer let me give you some advice about your code. Firstly in your while loop, since you check for a > 0 you don't have to check for a != 0 too, as it's included in the > operator. Secondly in your consecutive if statements, if a is bigger or equal to 0.25 it will enter the first if and it WILL NOT check the other ifs. Moreover if a is less than 0.25 it will check the second if, so you don't have to check again if a is less that 0.25. What I'm trying to say is you could write i like that: if (a >= 0.25) and then else if (a >= 0.10) and so on. Jul 10 '15 at 8:09
• Yeah. That makes sense. Thank you :) (y) Jul 10 '15 at 17:41
• It's nothing. You can also mark @Cliff B's answer as correct (by clicking the gray check icon next to the answer) if it helped you solve your problem. Jul 10 '15 at 17:42