# C Program to Convert Celsius to Fahrenheit

«
»
Problem Description

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

What is Celsius?
Celsius is a unit used to measure the temperature on Celsius scale. It was developed by Swedish astronomer Anders Celsius and is often denoted by °C(degree Celsius).

What is Celsius scale in C?
Celsius scale is based on freezing and boiling point of water at 1 atm pressure that is 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.

What is Fahrenheit?
Fahrenheit is also a unit to measure the temperature on Fahrenheit scale. It was developed by polish physicist Daniel Gabriel Fahrenheit in 1724 and is often denoted by symbol °F(degree Fahrenheit).

What is Fahrenheit scale?
On Fahrenheit scale the freezing and boiling point of water is defined at 32° and 212°F respectively with 180 divisions between them.

Problem Solution

Celsius to Fahrenheit conversion in C:

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$$\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}$$ Or $$F = \frac{9C}{5} + 32$$

Here, F is temperature in Fahrenheit and C is temperature is Celsius.

Example:
Let’s say the question goes like convert 37°C to Fahrenheit
So using the equation –

$$F = \frac{9C}{5} + 32$$

Substituting the values of C and solving the equation for F we have,

$$F = \frac{(9×37)}{5} + 32$$
$$F = \frac{333}{5} + 32$$
F = 66.6 + 32
F = 98.6°F

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

Method 1: Celsius to Fahrenheit in C using Naive Approach

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

Example:
Assume the temperature in Celsius is 100, then
$$F = \frac{9C}{5} + 32$$
$$F = \frac{9×100}{5} + 32$$
$$F = \frac{900}{5} + 32$$
F = 180 + 32
F = 212°F

Program/Source Code

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

1. Declare the double variable Celsius and Fahrenheit and take the input for temperature in Celsius using scanf function.
2. Calculate the temperature in Fahrenheit using the formula “Fahrenheit = (9 * Celsius) / 5 + 32;” and store it in variable named Fahrenheit
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 Celsius value “100” as input.

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

Testcase 2: In this case, we’re entering “37” as the Celsius value to convert to Fahrenheit.

Enter the Temperature in Celsius: 37
The Temperature in Fahrenheit is: 98.60

Method 2: Celsius to Fahrenheit in C using Function

In this approach, we convert the celsius value to fahrenheit using function. Functions enhance the code reusability and prevent users from violating the DRY principle which is don’t repeat yourself.

Example:
Assume the temperature in Celsius is 0, then
$$F = \frac{9C}{5} + 32$$
$$F = \frac{9×0}{5} + 32$$
$$F = \frac{900}{5} + 32$$
F = 0 + 32
F = 32°F

Program/Source Code

Here is source code of the C program to convert celsius to fahrenheit 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 celsius to fahrenheit using function
3.  */
4. 
5. #include <stdio.h>
6. 
7. void Celsius_to_Fahrenheit(double Celsius)
8. {
9.     double Fahrenheit;
10.     Fahrenheit = (9 * Celsius) / 5 + 32;
11. 
12.     printf ("The Temperature in Fahrenheit is: %.2lf\n", Fahrenheit);
13. }
14. 
15. int main()
16. {
17.     double Celsius;
18. 
19.     printf ("Enter the Temperature in Celsius: ");
20.     scanf ("%lf", &Celsius);
21. 
22.     Celsius_to_Fahrenheit(Celsius);
23. 
24.     return 0;
25. }
Program Explanation

1. Create a function name Celsius_to_Fahrenheit which take temperature in Celsius as parameter.
2. Calculate the temperature in Fahrenheit using the formula “Fahrenheit = (9 * Celsius) / 5 + 32;” and store it in variable named Fahrenheit
3. The return type of function is void as it does not return anything.
4. In driver program, declare double variable named Celsius and took the input using Scanf function and called the function Celsius_to_Fahrenheit by passing the temperature in Celsius as parameter
5. Print the result that is the temperature in Fahrenheit for the given temperature in Celsius using printf function.

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’re entering “0” as the Celsius value to convert to Fahrenheit.

Enter the Temperature in Celsius: 0
The Temperature in Fahrenheit is: 32.00

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

Enter the Temperature in Celsius: 12
The Temperature in Fahrenheit is: 53.60

Conclusion:

• Various temperature scales not only help to measure the temperatures but also to mathematically study them and perform important calculations based upon them.
• Celsius scale is quite popular in India whereas Fahrenheit Scale is popular in United States.

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