I am trying to write a simple C program that takes the input from user in metres and prints the output in kilometres, yards and miles rounded to 4 decimal places.
I have used this 1 meter = 0.001 kilometers = 1.094 yards = 0.0006215 miles
When I write this, I get the output as intended:
#include <stdio.h>
int main()
{
int m;
float km, y, mi;
scanf("%d",&m);
km=(m*0.001);
y=(m*1.094);
mi=(m*0.0006215);
printf("%.4f\n%.4f\n%.4f",km,y,mi);
return 0;
}
Output:
0.1000
109.4000
0.0622
But, when I write this I get the third output wrong:
#include <stdio.h>
int main()
{
int m;
scanf("%d",&m);
printf("%.4f\n%.4f\n%.4f",(m*0.001),(m*1.094),(m*0.0006215));
return 0;
}
Output:
0.1000
109.4000
0.0621
Can you please help me find the reason for this difference?
Edit-1
here I gave input 100.
float y=100*0.0006215;
printf("%f",y);
gave output 0.062150
and this
printf("%f",100*0.0006215);
also gave output 0.062150
Edit-2
float y=100*0.0006215;
printf("%.4f",y);
gave output 0.0622
while:
printf("%.4f",100*0.0006215);
gave output 0.0621
whereas in Edit-1 both displayed the same output.