Introduction :
Converting temperature between different units (Celsius, Fahrenheit and Kelvin) is a common task in programming. Converting temperature is an important work in many fields such as scientific research, engineering, weather forecasting and also our daily life. There are generally three temperature units. They are Celsius, Fahrenheit and Kelvin.
Using the C programming language, you can convert temperature from Celsius to Fahrenheit, Celsius to Kelvin, Fahrenheit to Celsius, Fahrenheit to Kelvin, Kelvin to Celsius and Kelvin to Fahrenheit. In this article, I shall discuss you how to convert temperature from one unit to another unit in C programming language.
What is temperature :
Any substance is hot or cold is measured by temperature. There are mainly three temperature units used in worldwide. They are Celsius (°C), Fahrenheit (°F) and Kelvin (K). The Celsius is the most used in the world. The Fahrenheit is used in the United States and Kelvin in scientific applications.
What is temperature conversion :
Converting the value of temperature from one unit to another unit is called temperature conversion. The following formulas are used for converting temperature from one unit to another unit.
- Convert Temperature Celsius to Fahrenheit —– fahrenheit = (celsius X 9/5) + 32
- Convert Temperature Celsius to Kelvin —– kelvin = celsius + 273.15
- Convert Temperature Fahrenheit to Celsius —– celsius = (fahrenheit-32) X 5/9
- Convert Temperature Fahrenheit to Kelvin —– kelvin = (fahrenheit – 32) X 5/9 +273.15
- Convert Temperature Kelvin to Celsius —– celsius = (kelvin – 273.15)
- Convert Temperature Kelvin to Fahrenheit —– fahrenheit = (kelvin -273.15) X 9/5+ 32
About the program :
The below program is a C program that converts temperature from one unit to another. In this program, I show you how you convert temperature between different units (Celsius, Fahrenheit and Kelvin) in C language. When you run the program, it asks you to input your choice from six different conversion options.
According to your choice, it converts the input temperature and display the result on the console screen. If you input an invalid choice, it displays an error message.
Explanation of the program :
In the program, first include standard input output library (stdio.h). Then, float variables (celsius, kelvin and fahrenheit) and integer variable (choice_unit) in the main() function. Here, you have to use printf() function to ask the user enter his choice of option for converting temperature. Using scanf() function, get the input data from the user.
Here, you can use “if else” statements to perform the appropriate conversion based on user input. According to user input, calculate the conversion and display the conversion value on the console screen.
How run the program :
First, install VS Code on your PC. Then, create a C file (“convert_temperature.c”) in VS Code. Now, copy the below code and paste in the C file that you have just created. After saving the file, run it. You can see the output on the console screen.
Source code of the program :
The following code is the source code of converting temperature from one unit to another unit in C programming language.
/* Develop by Puskar Jasu*/
#include <stdio.h>
int main()
{
float celsius, kelvin, fahrenheit;
int choice_unit;
printf("Please enter your choice for convert temperature: \nEnter 1 for Convert Temperature Celsius to Fahrenheit\nEnter 2 for Convert Temperature Celsius to Kelvin\nEnter 3 for Convert Temperature Fahrenheit to Celsius\nEnter 4 for Convert Temperature Fahrenheit to Kelvin\nEnter 5 for Convert Temperature Kelvin to Celsius\nEnter 6 for Convert Temperature Kelvin to Fahrenheit\n");
scanf("%d", &choice_unit);
if (choice_unit == 1)
{
printf("For Convert Temperature Celsius to Fahrenheit Enter Temperature\n");
scanf("%f", &celsius);
fahrenheit = (celsius * 9 / 5) + 32;
printf("%.2f Celsius equal to %.2f Fahrenheit\n", celsius, fahrenheit);
}
else if (choice_unit == 2)
{
printf("For Convert Temperature Celsius to Kelvin Enter Temperature\n");
scanf("%f", &celsius);
kelvin = celsius + 273.15;
printf("%.2f Celsius equal to %.2f Kelvin\n", celsius, kelvin);
}
else if (choice_unit == 3)
{
printf("For Convert Temperature Fahrenheit to Celsius Enter Temperature\n");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 5 / 9;
printf("%.2f Fahrenheit equal to %.2f Celsius\n", fahrenheit, celsius);
}
else if (choice_unit == 4)
{
printf("For Convert Temperature Fahrenheit to Kelvin Enter Temperature\n");
scanf("%f", &fahrenheit);
kelvin = (fahrenheit - 32) * 5 / 9 + 273.15;
printf("%.2f Fahrenheit equal to %.2f Kelvin\n", fahrenheit, kelvin);
}
else if (choice_unit == 5)
{
printf("For Convert Temperature Kelvin to Celsius Enter Temperature\n");
scanf("%f", &kelvin);
celsius = (kelvin - 273.15);
printf("%.2f Kelvin equal to %.2f Celsius\n", kelvin, celsius);
}
else if (choice_unit == 6)
{
printf("For Convert Temperature Kelvin to Fahrenheit Enter Temperature\n");
scanf("%f", &kelvin);
fahrenheit = (kelvin - 273.15) * 9 / 5 + 32;
printf("%.2f Kelvin equal to %.2f Fahrenheit\n", kelvin, fahrenheit);
}
else
{
printf("Please! enter your choice 1 to 6\n");
}
return 0;
}
Output of the program :
If you run the program, you can see the following output like below image.

Conclusion :
In this above article, I have discussed how to convert temperature in C programming language. I think you would be understood how to convert temperature in various units. Thank you for visiting my site.