Temperature Conversion Program in C

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 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.

Problem Description

Write a C program that takes a celsius value as input and convert it to a fahrenheit value.

Problem Solution

Convert the units to other units using basic mathematical calculations.

These mathematical equations are:

advertisement
advertisement
\(\frac{K -273.15}{100}\) = \(\frac{C-0}{100}\) = \(\frac{F-32}{180}\)

K = Temperature in Kelvin.
C = Temperature in Celsius.
F = Temperature in Fahrenheit.

Let’s discuss different ways to convert temperature conversion in C language.

Method 1: (Using Switch Case)

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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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.

advertisement
  • 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

advertisement

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

Program/Source Code

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.

  1. /*
  2.  * C program to perform temperature conversion using switch case
  3.  */
  4.  
  5. #include<stdio.h>
  6.  
  7. int main()
  8. {
  9.     float Celsius, Kelvin, Fahrenhiet;
  10.     int choice;
  11.     printf("Choose the unit you want to convert: \n");
  12.     printf("1.Kelvin\n");
  13.     printf("2.Celsius\n");
  14.     printf("3.Fahrenhiet \n");
  15.     scanf("%d", &choice);
  16.     switch (choice)
  17.     {
  18.     case 1:
  19.         printf("Enter the temperature in kelvin: ");
  20.         scanf("%f",&Kelvin);
  21.         Celsius = (Kelvin - 273.15);
  22.         Fahrenhiet = 1.8 *(Kelvin -273.15) + 32.0;
  23.         printf("In Celsius the value is:\t %f \n",Celsius);
  24.         printf("In Fahrenhiet the value is:\t %f",Fahrenhiet);
  25.         break;
  26.     case 2:
  27.         printf("Enter the temperature in Celsius: ");
  28.         scanf("%f",&Celsius);
  29.         Kelvin = (Celsius + 273.15);
  30.         Fahrenhiet = (Celsius * 1.8) + 32.0;
  31.         printf("In kelvin the value is : %f \n",Kelvin);
  32.         printf("In Fahrenhiet the value is: %f",Fahrenhiet);
  33.         break;
  34.     case 3:
  35.         printf("Enter the temperature in fahrenhite: ");
  36.         scanf("%f",&Fahrenhiet);
  37.         Kelvin = (Fahrenhiet - 32.0) * 5/9 +273.15;
  38.         Celsius = (Fahrenheit - 32.0) * 5/9;
  39.         printf("In Celsius the value is :\t %f \n",Celsius);
  40.         printf("In Kevlin the value is :\t %f \n",Kelvin);
  41.         break;
  42.     default:
  43.         printf("Please! enter a valid value \n");
  44.         break;
  45.     }
  46.     printf("\n\n");
  47.     return 0;
  48. }
Program Explanation

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.

Runtime Test Cases

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

Method 2: (Using Function like Macros)

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.

Program/Source Code

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.

  1. /*
  2.  * C program to perform temperature conversion using function (macros)
  3.  */
  4.  
  5. #include<stdio.h>
  6. //FUNCTION LIKE MACROS IN C LANGUAGE ->
  7. #define CelsiusToKelvin(Celsius)(Celsius + 273.15)
  8. #define CelsiusToFahrenheit(Celcius)( (Celsius * 1.8) + 32.0)
  9.  
  10. #define KelvintoCelsius(Kelvin) (Kelvin - 273.15)
  11. #define KelvinToFahrenheit(Kelvin)((Kelvin - 273.15) * 1.8 + 32.0)
  12.  
  13. #define FahrenheitToCelsius(Fahrenheit) ((Fahrenheit - 32.0) * 5/9)
  14. #define FahrenhietToKelvin(Fahrenheit) ((Fahrenheit - 32.0) * 5/9 + 273.15)
  15.  
  16. int main()
  17. {
  18.     float Celsius, Kelvin, Fahrenheit;
  19.  
  20.     Celsius = 150.0;
  21.     printf("150-degree   Celsius  = %f-degree   Kelvin \n ",CelsiusToKelvin(Celsius));
  22.     printf("150-degree   Celsius =  %f-degree Fahrenheit\n ",CelsiusToFahrenheit(Celsius));
  23.  
  24.     Kelvin = 150.0;
  25.     printf("150-degree   Kelvin  = %f-degree   Celsius \n",KelvintoCelsius(Kelvin));
  26.     printf("150-degree   Kelvin  = %f-degree Fahrenheit \n",KelvinToFahrenheit(Kelvin));
  27.  
  28.     Fahrenheit = 150.0;
  29.     printf("150-degree Fahrenheit =  %f-degree   Celsius \n",FahrenheitToCelsius(Fahrenheit));
  30.     printf("150-degree Fahrenheit =  %f-degree   Kelvin \n",FahrenhietToKelvin(Fahrenheit));
  31.     return 0;
  32. }
Program Explanation

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.

Runtime Test Cases
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”.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.