# C program to Convert Fahrenheit to Celsius

Problem Description

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

What is Fahrenheit and Fahrenheit scale?
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.

What is Celsius and Celsius scale?
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.

Problem Solution

Fahrenheit to Celsius conversion:

The conversion of Fahrenheit to Celsius takes place by the given relation –

$$\frac{C-0}{100-0}=\frac{F-32}{212-32}$$

Which on further simplifying gives –

$$\frac{C}{100}=\frac{F-32}{180}$$

$$C=\frac{5(F-32)}{9} = (F-32)*0.555555$$

$$\frac{5}{9}$$=0.555555
Here, F is temperature in Fahrenheit and C is temperature is Celsius.

Example:
Let’s say the question goes like convert 100°F to Celsius
So using the equation –

$$C = (F – 32) * \frac{5}{9}$$

Substituting the value by solving the equation for C we have,

$$C = (F – 32) * \frac{5}{9}$$
$$C = (100 – 32) * \frac{5}{9}$$
C = (100 – 32) * 0.555555
C = 68 * 0.55555
C = 37.78°C

Let’s discuss different ways to convert fahrenheit to celsius in C language.

Method 1: (Naive Approach)

This approach simply converts the fahrenheit to celsius using the formula i.e. Celsius = (Fahrenheit – 32) * 5/9

Example:
Assume the temperature in Fahrenheit is 70, then
$$C = (F – 32) * \frac{5}{9}$$
$$C = (70 – 32) * \frac{5}{9}$$
C = 38 * 0.55555
C = 21.11°C

Program/Source Code

Here is source code of the C program to convert fahrenheit to celsius using formula. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

1. /*
2.  * C program to convert fahrenheit to celsius using formula
3.  */
4. 
5. #include <stdio.h>
6. int main()
7. {
8.     double Fahrenheit, Celsius;
9. 
10.     printf ("Enter the Temperature in Fahrenheit: ");
11.     scanf ("%lf", & Fahrenheit);
12. 
13.     //Formula to convert fahrenheit to celsius
14.     Celsius = (Fahrenheit - 32) * 5/9;
15.     printf ("The Temperature in Celsius is: %.2lf\n", Celsius);
16.     return 0;
17. }
Program Explanation

1. Declare the double variable Celsius and Fahrenheit and take the input for temperature in Fahrenheit using the scanf function.
2. Calculate the temperature in Celsius using the formula “Celsius = (Fahrenheit – 32) * 5/9” and store it in variable named Celsius
3. Finally, print the result using printf function.

Note:

• The declared data-type of variable is double because temperatures can be in decimal and the format specifier for double is %lf.
• %.2lf in above code prints the output till two decimal digits.

Time Complexity: O(1)
The Time Complexity of above program is O(1), as the code is taking constant time in calculating the result based on the formula.

Space Complexity: O(1)
The space complexity is O(1), as no any extra/auxiliary space is required in the program.

Runtime Test Cases

Testcase 1: In this case, we are entering the Fahrenheit value “100” as input.

Enter the Temperature in Fahrenheit: 100
The Temperature in Celsius is: 37.78

Testcase 2: In this case, we are entering the Fahrenheit value “70” as input.

Enter the Temperature in Fahrenheit: 70
The Temperature in Celsius is: 21.11

Method 2: (Using Functions 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.

Example:
Assume the temperature in Fahrenheit is 45, then
$$C = (F – 32) * \frac{5}{9}$$
$$C = (45 – 32) * \frac{5}{9}$$
C = 13 * 0.55555
C = 7.22°C

Program/Source Code

Here is source code of the C program to convert fahrenheit to celsius 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 convert fahrenheit to celsius using function (macros)
3.  */
4. 
5. #include<stdio.h>
6. 
7. //Function Like Macros in C Language
8. #define FahrenheitToCelsius(Fahrenheit) ((Fahrenheit - 32.0) * 5/9)
9. int main()
10. {
11.     float Celsius, Fahrenheit;
12.     printf("Enter the Temperature in Fahrenheit: ");
13.     scanf(“%f",&Fahrenheit);
14.     printf("The Temperature in Celsius is: ");
15.     printf("%f\n",FahrenheitToCelsius(Fahrenheit));
16.     return 0;
17. }
Program Explanation

1. Declare the double variable Celsius and Fahrenheit and take the input for temperature in Fahrenheit using the scanf function.
2. Then we scan the value in units of Fahrenheit and convert it to two other units using a macro.
3. The compiler processes the code and replaces the macro name with an expression i.e. i.e. #define FahrenheitToCelsius(Fahrenheit) ((Fahrenheit – 32.0) * 5/9)
4. The compiler then finds the value of the expression and prints its value.

Time Complexity: O(1)
The Time Complexity of above program is O(1), as the code is taking constant time in calculating the result based on the formula.

Space Complexity: O(1)
The space complexity is O(1), as no any extra/auxiliary space is required in the program.

Runtime Test Cases

Testcase 1: In this case, we are entering the Fahrenheit value “100” as input.

Enter the Temperature in Fahrenheit: 100
The Temperature in Celsius is: 37.78

Testcase 2: In this case, we are entering the Fahrenheit value “45” as input.

Enter the Temperature in Fahrenheit: 45
The Temperature in Celsius is: 7.22

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

If you find any mistake above, kindly email to [email protected]