Hello, I think there's something wrong in my code. I made a program converting °C-°F . the formula is °F = (1.8*°C) + 32 Here's the code: Code: #include <stdio.h> #include <conio.h> int main() { int C; int F; printf("Enter the value of °C: "); scanf("%d", &C); total = (1.8 * C) + 32; printf("°F=%d", F); getch(); } when I enter the value of C as 2. the answer should be 35.6 .. but it only shows 35.. it doesn't have any decimals.. what should I do?? thank you in advance if someone will help..
ahh no.. it's wrong.. here's the correct one.. Code: #include <stdio.h> #include <conio.h> int main() { int C; int F; printf("Enter the value of °C: "); scanf("%d", &C); F = (1.8 * C) + 32; printf("°F=%d", F); getch(); }
TO get the output in decimals we need to use the 'float' data-type . Corrected Code :- Code: #include <stdio.h> #include <conio.h> int main() { int C; [COLOR="Red"]float F;[/COLOR] printf("Enter the value of °C: "); scanf("%d", &C); F = (1.8 * C) + 32; [COLOR="Red"]printf("°F=%f", F);[/COLOR] getch(); } Output :- Code: Enter the value of C: 2 F=35.599998
what u(jam 143) have writen is absolutely correct but............... there is only one controversy that your answer will not be printed in decimals because int range is-32768to 32767 so it cannot accept decimal values so we use other data type called "float" whose range is -3.4e-38 to -3.4e+38 so we can use it just print the answer as printf("F=%f",f); you will get the required answer
Iam sorry if am wrong about the above program how can int f; give you floating point value if u write %f.
yes sir, it will show abnormal termination what i mean in before post is to cast the data type for example x=(float)y;