C | Data Types | Question 3

Predict the output

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
int main()
{
   float c = 5.0;
   printf ("Temperature in Fahrenheit is %.2f", (9/5)*c + 32);
   return 0;
}

chevron_right


(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.



filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
int main()
{
   float c = 5.0;
   printf ("Temperature in Fahrenheit is %.2f", (9.0/5)*c + 32);
   return 0;
}

chevron_right



Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.