Convert temperature from Fahrenheit to Celsius and vice versa
C program to convert temperature from Fahrenheit to Celsius and vice versa.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
float faren,cels;
int choice;
printf("\n1: Convert temperature from Fahrenheit to Celsius.");
printf("\n2: Convert temperature from Celsius to Fahrenheit.");
printf("\nEnter your choice\t: ");
scanf("%d",&choice);
if(choice ==1)
{
printf("\nEnter temperature in Fahrenheit\t: ");
scanf("%f",&faren);
cels= (faren - 32) / 1.8;
printf("Temperature in Celsius: %.2f",cels);
}
else if(choice==2)
{
printf("\nEnter temperature in Celsius\t: ");
scanf("%f",&cels);
faren= (cels * 1.8) + 32;
printf("Temperature in Fahrenheit: %.2f",faren);
}
else
{
printf("\nInvalid Choice !!!");
}
getch();
}
Output
********** Run1 **********
1: Convert temperature from Fahrenheit to Celsius.
2: Convert temperature from Celsius to Fahrenheit.
Enter your choice : 1
Enter temperature in Fahrenheit : 156
Temperature in Celsius: 68.89
********** Run1 **********
1: Convert temperature from Fahrenheit to Celsius.
2: Convert temperature from Celsius to Fahrenheit.
Enter your choice : 2
Enter temperature in Celsius : 100
Temperature in Fahrenheit: 212.00
Explanation
In the above program, user is given two options: whether to convert temperature from Fahrenheit to Celsius or from Celsius to Fahrenheit. Depending on the choice entered, condition is checked inside if else statements.
Temperature in Fahrenheit is converted into Celsius using the formula:
Temperature in Celsius is converted into Fahrenheit using the formula:
Temperature in Fahrenheit is converted into Celsius using the formula:
cels= (faren - 32) / 1.8;
Temperature in Celsius is converted into Fahrenheit using the formula:
faren= (cels * 1.8) + 32;