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; }
8 ответов
+ 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
+ 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)));
+ 2
Change it to int to float and %d to %f.
+ 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
+ 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
0
fahrenheit is undefined in this code. Scan a value into it first, then do the arithmetic
0
I changed it, but still doesnt work
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;
}