Puzzle Code:
#include<stdio.h> #include<conio.h> void main() { int c; float a=0.41; c=a*100; printf("%d",c); getch(); }
The output of the above code is 40 not 41. Why?
(Solution in comments)
Puzzle Code:
#include<stdio.h> #include<conio.h> void main() { int c; float a=0.41; c=a*100; printf("%d",c); getch(); }
The output of the above code is 40 not 41. Why?
(Solution in comments)
1 Comment
Solution: Compiler never stores an exact float value in the memory. An approximate value of the float is stored. Example in above case a value would be like 0.408235 or so which is approx to 0.41. When this value is multiplied by 100 and stored in an int it becomes 40, which is the output.