0

Why am I not getting the output?

I want to convert F to C. when the user enters the temperature in F, then the program will convert it to celsius. But my output it's always 0. #include <stdio.h> // convert fahrenheit to celsius int main() { int fahrenheit; int celsius; celsius = (5/9*(fahrenheit - 32)); scanf("%d", &fahrenheit); printf("%d", celsius); return 0; }

22nd Jan 2022, 12:09 AM
Altesse
8 Answers
+ 4
In C, a/b = 0 when a<b and a,b are integers. Try using floats instead 5/9.0 or 5.0/9
22nd Jan 2022, 3:23 AM
Simba
Simba - avatar
+ 2
You are running into an integer math problem with ratios like 5/9. This is because in int math 5/9 = 0 Chang int to float and %d to %f. int farh; scanf("%d", &farh); printf("%f", (5.0/9.0*((float)farh-32)));
22nd Jan 2022, 7:49 AM
William Owens
William Owens - avatar
+ 2
Change it to int to float and %d to %f.
23rd Jan 2022, 4:14 AM
đŸ’» Code_02🩁👑
đŸ’» Code_02🩁👑 - avatar
+ 1
Might have something to do with the rounding. I wouldn't know cause I'd need to see the whole problem again. And what did you change? You can copy the code to the playground, link it here, and then update it as people try and help and they'll be able to see what you're doin
22nd Jan 2022, 12:46 AM
Slick
Slick - avatar
+ 1
I think it's a mathematical error Basically, if the user inputs a number a which is lower than b, then the answer will be negative... And negative/positive = math error
23rd Jan 2022, 1:41 PM
Dennis Kimathi
Dennis Kimathi - avatar
0
fahrenheit is undefined in this code. Scan a value into it first, then do the arithmetic
22nd Jan 2022, 12:34 AM
Slick
Slick - avatar
0
I changed it, but still doesnt work
22nd Jan 2022, 12:42 AM
Altesse
0
#include <stdio.h> // convert fahrenheit to celsius int main() { int fahrenheit; int celsius; scanf("%d", &fahrenheit); celsius = (5/9*(fahrenheit - 32)); printf("%d", celsius); return 0; }
22nd Jan 2022, 1:04 AM
Altesse