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.
a > 0
you don't have to check fora != 0
too, as it's included in the>
operator. Secondly in your consecutive if statements, ifa
is bigger or equal to 0.25 it will enter the first if and it WILL NOT check the other ifs. Moreover ifa
is less than 0.25 it will check the second if, so you don't have to check again ifa
is less that 0.25. What I'm trying to say is you could write i like that:if (a >= 0.25)
and thenelse if (a >= 0.10)
and so on.