There are many ways to measure temperature, but the three most common units of temperature measurement are Kelvin, Celsius, and Fahrenheit.
Fahrenheit is a unit to measure the temperature on Fahrenheit scale. It is expressed in °F (degree Fahrenheit). On the Fahrenheit scale, the freezing point and boiling point of water are defined as 32° and 212°F, with 180 degrees divided between them.
Celsius is also a unit used to measure the temperature on Celsius scale. It is expressed in °C (degree Celsius). The celsius scale is based on the freezing and boiling points of water at a pressure of 1 atm, which are 0° and 100°C respectively. The scale is divided into 100 divisions between the two extreme temperatures of water and hence it is often referred to as centigrade scale.
The Kelvin temperature scale is an absolute temperature scale determined by the thermodynamic third law. It is expressed in K. The Kelvin unit is defined as the distance between absolute zero and the triple point of water.
Write a C program that takes a celsius value as input and convert it to a fahrenheit value.
Convert the units to other units using basic mathematical calculations.
These mathematical equations are:
K = Temperature in Kelvin.
C = Temperature in Celsius.
F = Temperature in Fahrenheit.
Let’s discuss different ways to convert temperature conversion in C language.
- Temperature Conversion in C using Switch Case
- Temperature Conversion in C using Function like Macros
A switch-case is a multi-directional conditional control statement that is used as needed in the program to match choices between several alternatives. To make this selection, we use the switch statement.
Example 1:
If the temperature is in kelvin, use the following formulas to calculate celsius and fahrenhiet.
- Celsius = (Kelvin – 273.15);
- Fahrenhiet = 1.8 *(Kelvin -273.15) + 32.0;
Given Kelvin = 100, then
C = (Kelvin – 273.15)
C = (100 – 273.15)
C = -173.149994
F = 1.8*(Kelvin -273.15) + 32.0
F = 1.8*(100 – 273.15) + 32
F = -311.670013 + 32
F = -279.670013
Example 2:
If the temperature is in Celsius, use the following formulas to calculate Fahrenheit and Kelvin.
- Kelvin = (Celsius + 273.15);
- Fahrenhiet = (Celsius * 1.8) + 32.0;
Given Celsius = 453, then
K = (Celsius + 273.15)
K = (453 + 273.15)
K = 726.15
F = (Celsius * 1.8) + 32.0
F = (453 * 1.8) + 32
F = 815.4 + 32
F = 847.40
Example 3:
If the temperature is in Fahrenheit, use the following formulas to calculate Celsius and Kelvin.
- Kelvin = (Fahrenhiet – 32.0) * 0.555555 +273.15;
- Celsius = (Fahrenhiet – 32.0) * 0.555555;
Given Fahrenheit = -45, then
K = (Fahrenhiet – 32.0) * 0.555555 +273.15
K = (-45 – 32.0) * 0.555555 + 273.15
K = -77 * 0.555555 + 273.15
K = 230.37
C = (Fahrenhiet – 32.0) * 0.555555
C = (-45 – 32.0) * 0.555555
C = -77 * 0.555555
C = -42.777
Here is source code of the C program to perform temperature conversion using switch case. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to perform temperature conversion using switch case
*/
#include<stdio.h>
int main()
{
float Celsius, Kelvin, Fahrenhiet;
int choice;
printf("Choose the unit you want to convert: \n");
printf("1.Kelvin\n");
printf("2.Celsius\n");
printf("3.Fahrenhiet \n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the temperature in kelvin: ");
scanf("%f",&Kelvin);
Celsius = (Kelvin - 273.15);
Fahrenhiet = 1.8 *(Kelvin -273.15) + 32.0;
printf("In Celsius the value is:\t %f \n",Celsius);
printf("In Fahrenhiet the value is:\t %f",Fahrenhiet);
break;
case 2:
printf("Enter the temperature in Celsius: ");
scanf("%f",&Celsius);
Kelvin = (Celsius + 273.15);
Fahrenhiet = (Celsius * 1.8) + 32.0;
printf("In kelvin the value is : %f \n",Kelvin);
printf("In Fahrenhiet the value is: %f",Fahrenhiet);
break;
case 3:
printf("Enter the temperature in fahrenhite: ");
scanf("%f",&Fahrenhiet);
Kelvin = (Fahrenhiet - 32.0) * 5/9 +273.15;
Celsius = (Fahrenheit - 32.0) * 5/9;
printf("In Celsius the value is :\t %f \n",Celsius);
printf("In Kevlin the value is :\t %f \n",Kelvin);
break;
default:
printf("Please! enter a valid value \n");
break;
}
printf("\n\n");
return 0;
}
1. First, we initialized the variable for storing the temperature in different units. These units are floating point numbers.
2. Then we asked the user what units the temperature was recorded in.
3. By using a switch case, we switched to a case where we could find and print the equivalents of the other two unknown units.
4. Then enter the temperature value of the known unit.
5. Find the value of the other two units using mathematical calculations.
6. If temperature is given in kelvin, then calculate celsius and fahrenhiet using below formula
- Celsius = (Kelvin – 273.15);
- Fahrenhiet = 1.8 *(Kelvin -273.15) + 32.0;
7. If the temperature is in Fahrenheit, use the following formulas to calculate Celsius and Kelvin.
- Kelvin = (Fahrenhiet – 32.0) * 0.555555 +273.15;
- Celsius = (Fahrenhiet – 32.0) * 0.555555;
8. If the temperature is in Celsius, use the following formulas to calculate Fahrenheit and Kelvin.
- Kelvin = (Celsius + 273.15);
- Fahrenhiet = (Celsius * 1.8) + 32.0;
Time Complexity: O(1)
The time complexity of the above program is O(1), as it has no loops or recursion.
Space Complexity: O(k)
Space complexity of the above program is O(k), where k = constant.
Testcase 1: In this case, we are entering the kelvin value “100” as input.
Choose the unit you want to convert: 1.Kelvin 2.Celsius 3.Fahrenhiet 1 Enter the temperature in kelvin: 100 In Celcius the value is: -173.149994 In Fahrenhiet the value is: -279.670013
Testcase 2: In this case, we are entering the Celsius value “453” as input.
Choose the unit you want to convert: 1.Kelvin 2.Celsius 3.Fahrenhiet 2 Enter the temperature in Celsius: 453 In kelvin the value is : 726.150024 In Fahrenhiet the value is: 847.400024
Testcase 3: In this case, we are entering the Fahrenhiet value “-45” as input.
Choose the unit you want to convert: 1.Kelvin 2.Celsius 3.Fahrenhiet 3 Enter the temperature in Fahrenhite: -45 In Celsius the value is : -42.777737 In Kevlin the value is : 230.372269
The #define directive can also be used to define macros with arguments. The general syntax is- #define macro name (argument 1, argument 2, ……) (expression)
Argument 1, and argument 2 are the formal arguments that we use in our function-like macros.
The macro name is replaced by the expression, and the formal arguments are also being replaced by the corresponding actual arguments in the macro call.
For example, # define sum (a, b) (a + b) -> This is a pre-processor directive.
sum(argument1, arguement2) is used in the program. sum(argument1, arguement2) is replaced by argument1 + argument2.
This style can be used to convert temperature units such as kelvin, Fahrenheit, and Celsius.
Here is source code of the C program to perform temperature conversion using function (macros). The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to perform temperature conversion using function (macros)
*/
#include<stdio.h>
//FUNCTION LIKE MACROS IN C LANGUAGE ->
#define CelsiusToKelvin(Celsius)(Celsius + 273.15)
#define CelsiusToFahrenheit(Celcius)( (Celsius * 1.8) + 32.0)
#define KelvintoCelsius(Kelvin) (Kelvin - 273.15)
#define KelvinToFahrenheit(Kelvin)((Kelvin - 273.15) * 1.8 + 32.0)
#define FahrenheitToCelsius(Fahrenheit) ((Fahrenheit - 32.0) * 5/9)
#define FahrenhietToKelvin(Fahrenheit) ((Fahrenheit - 32.0) * 5/9 + 273.15)
int main()
{
float Celsius, Kelvin, Fahrenheit;
Celsius = 150.0;
printf("150-degree Celsius = %f-degree Kelvin \n ",CelsiusToKelvin(Celsius));
printf("150-degree Celsius = %f-degree Fahrenheit\n ",CelsiusToFahrenheit(Celsius));
Kelvin = 150.0;
printf("150-degree Kelvin = %f-degree Celsius \n",KelvintoCelsius(Kelvin));
printf("150-degree Kelvin = %f-degree Fahrenheit \n",KelvinToFahrenheit(Kelvin));
Fahrenheit = 150.0;
printf("150-degree Fahrenheit = %f-degree Celsius \n",FahrenheitToCelsius(Fahrenheit));
printf("150-degree Fahrenheit = %f-degree Kelvin \n",FahrenhietToKelvin(Fahrenheit));
return 0;
}
1. First declare the variables and store the value of temperatures for different units of conversion.
2. Then initialize the value for 1 unit and convert it to two other units using macros.
3. The macros name is replaced by the expression.
4. The compiler processes the code and replaces the macro name with expression.
5. After that compiler finds the value of the expression and prints it.
Time Complexity: O(1)
The time complexity of the above program is O(1), as it has no loops or recursion.
Space Complexity: O(k)
Space complexity of the above program is O(k), where k = constant.
150-degree Celsius = 423.150000-degree Kelvin 150-degree Celsius = 302.000000-degree Fahrenheit 150-degree Kelvin = -123.150000-degree Celsius 150-degree Kelvin = -189.670000-degree Fahrenheit 150-degree Fahrenheit = 65.555490-degree Celsius 150-degree Fahrenheit = 338.705490-degree Kelvin
To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.
- Watch Advanced C Programming Videos
- Check C Books
- Practice Computer Science MCQs
- Practice BCA MCQs
- Apply for C Internship