I am working on C program with float values, below is my code.
#include<stdio.h>
#include<stdlib.h>
#include<float.h>
int main()
{
int counter = 0;
float quarter = 0.25;
float dime = 0.10;
float nickel = 0.05;
float penny = 0.01;
float change = 0.00;
printf("hi, how much do i owe u?\t");
scanf("%f", &change);
while(change > 0.0)
{
if(change >= quarter)
{
change -= quarter;
printf("quarter %.2f\n", quarter);
}
else if(change >= dime)
{
change -= dime;
printf("dime %.2f\n", dime);
}
else if(change >= nickel)
{
change -= nickel;
printf("nickel %.2f\n", nickel);
}
else if(change >= penny)
{
change -= penny;
printf("penny %.2f\n", penny);
}
counter++;
}
printf("your count is %i\n", counter);
return 0;
}
Output is:
hi, how much do i owe u? .45
quarter 0.25
dime 0.10
nickel 0.05
penny 0.01
penny 0.01
penny 0.01
penny 0.01
`^C`
I have to press ctrl
c
to terminate loop
Last printf("your count is %i\n", counter);
does not execute at all - to count # of coins
used
If i replace float
type with int
it works OK.
Please help with this problem