Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed why the output coming 37 not 41?

+10 votes
asked Nov 13, 2021 by Abhiraj Das (220 points)
closed Nov 19, 2021 by Admin
include <stdio.h>

int main()
{
float c = 5.0;
printf ("Temperature in Fahrenheit is %.2f", (9/5)*c + 32);
return 0;
}
closed with the note: answered

5 Answers

+4 votes
answered Nov 13, 2021 by Peter Minarik (84,720 points)
edited Nov 15, 2021 by Peter Minarik
(9 / 5) * 5.0 + 32 = (int / int) * float + int
'--v--'              '----v----'
   1    * 5.0 + 32 =     int     * float + int
   '----v---'            '------v------'
       5.0    + 32 =          float      + int
       '----v----'            '-------v------'
           37.0    =                float

9/5 is an integer division (all fractions are ignored) which results in 1. If you want a float division (fractions included), then you have to have either the nominator or the denominator to be a float (or double). Either explicitly cast them to float ((float)9) or use a floating-point literal (e.g. 9.0 or 5.0), not an integral literal (9, 5).

Another solution is to use implicit casting, that is to let the compiler cast your number automatically due to operator precedence. In this case, you could reorder your expression to be something like this:

c * 9 / 5 + 32 = float * int / int + int --> float

This way, due to operator precedence, the result of each operation is a float, not an int.

For clarity, probably the best solution is to always use floating-point literals to explicitly state that you want the result to be a floating-point number and not an integral number.

0 votes
answered Nov 14, 2021 by dhrup kr sinha (270 points)
//please write your code properly, Your formula was wrong.

#include <stdio.h>

int main()
{
float c = 5.0;
printf ("Temperature in Fahrenheit is %f", (c*9/5) + 32);  /*(9/5)*c+32) is wrong formula*/
return 0;
}
commented Nov 16, 2021 by Nilesh (160 points)
#include <stdio.h>

int main()
{
float c = 5.0;
printf ("Temperature in Fahrenheit is %.2f", (9.0/5.0)*c + 32);
return 0;
}
0 votes
answered Nov 17, 2021 by Nilesh (160 points)

#include <stdio.h>

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

0 votes
answered Nov 17, 2021 by Harshit Shukla (140 points)

because its divide first 9/5 

correct program is 

#include <stdio.h>

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

0 votes
answered Nov 18, 2021 by Z-REHMAN INFOTECH (140 points)
bcz when we divide 9/5 so anwer is 1 bcz it's a integer number then we multiply with c is float 5.0 is its a 5 and we add 32 so answer is 37.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...