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 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.
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.
Celsius to Fahrenheit conversion in C:
The conversion of Celsius to Fahrenheit takes place by the given relation –
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 –
Substituting the values of C and solving the equation for F we have,
\(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.
- Convert Celsius to Fahrenheit in C using Naive Approach
- Convert Celsius to Fahrenheit in C using Function
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
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.
/*
* C program to convert celsius to fahrenheit using formula
*/
#include <stdio.h>
int main()
{
double Fahrenheit;
double Celsius;
printf ("Enter the Temperature in Celsius: ");
scanf ("%lf", &Celsius);
Fahrenheit = (9 * Celsius) / 5 + 32;
printf ("The Temperature in Fahrenheit is: %.2lf\n", Fahrenheit);
return 0;
}
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.
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
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
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.
/*
* C program to convert celsius to fahrenheit using function
*/
#include <stdio.h>
void Celsius_to_Fahrenheit(double Celsius)
{
double Fahrenheit;
Fahrenheit = (9 * Celsius) / 5 + 32;
printf ("The Temperature in Fahrenheit is: %.2lf\n", Fahrenheit);
}
int main()
{
double Celsius;
printf ("Enter the Temperature in Celsius: ");
scanf ("%lf", &Celsius);
Celsius_to_Fahrenheit(Celsius);
return 0;
}
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.
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”.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice BCA MCQs
- Practice Computer Science MCQs
- Buy C Books
- Apply for Computer Science Internship
- Buy Computer Science Books