Before going to the program first let us see how to convert Celsius to Fahrenheit?
To convert from Celsius to Fahrenheit: first multiply by 180/100, then add 32.
But 180/100 can be simplified to 9/5, so this is the easiest way:
°C to °F: Multiply by 9, then divide by 5, then add 32
So now we can write a formula like this:
Celsius to Fahrenheit: (°C × 9/5) + 32 = °F
Other Method: Use 1.8 instead of 9/5
9/5 is equal to 1.8, so we can also use this method.
Celsius to Fahrenheit: °C × 1.8 + 32 = °F
Example: Convert 41° Celsius to Fahrenheit
First: 41° × 1.8 = 73.8
Then: 73.8 + 32 = 105.8° F
Program code to convert Celsius to Fahrenheit in C:
#include<stdio.h> #include<conio.h> void main() { float cels,fah; clrscr(); printf("Enter the temperature in Celsius:"); scanf("%f",&cels); fah=(1.8*cels)+32; printf("nConverted Temperature is %.2f%cF",fah,248); getch(); }
Explanation:
- First the computer reads the temperature in Celsius from the user and stores it in the “cels” variable using the following lines:
printf("Enter the temperature in Celsius:"); scanf("%f",&cels);
Note: %f is used to read the floating-point value (decimal value).
- Then using the formula the Fahrenheit value is calculated and stored in the “fah” variable using the following line:
fah=(1.8*cels)+32;
- Finally the Fahrenheit value is printed on the screen using the following line:
printf("nConverted Temperature is %.2f%cF",fah,248);
Note: %.2f is used to print the floating-point value with only 2 decimal places,
%c is used to print a character and
248 is the ASCII value of the character ” º “(degree).