Predict the output
#include <stdio.h> int main()
{ float c = 5.0;
printf ( "Temperature in Fahrenheit is %.2f" , (9/5)*c + 32);
return 0;
} |
(A) Temperature in Fahrenheit is 41.00
(B) Temperature in Fahrenheit is 37.00
(C) Temperature in Fahrenheit is 0.00
(D) Compiler Error
Answer: (B)
Explanation: Since 9 and 5 are integers, integer arithmetic happens in subexpression (9/5) and we get 1 as its value.
To fix the above program, we can use 9.0 instead of 9 or 5.0 instead of 5 so that floating point arithmetic happens.
#include <stdio.h> int main()
{ float c = 5.0;
printf ( "Temperature in Fahrenheit is %.2f" , (9.0/5)*c + 32);
return 0;
} |