In this article, we will discuss how to write a C program to convert temperature from Celsius to Fahrenheit. We will provide a step-by-step explanation and include code examples to make it easy for beginners to understand the concept. So, let’s get started!
Contents
Understanding the Conversion Formula
Before we dive into the code, let’s understand the formula used to convert temperature from Celsius to Fahrenheit. The formula is as follows:
F = (C * 9/5) + 32
Where:
- F represents the temperature in Fahrenheit.
- C represents the temperature in Celsius.
To convert Celsius to Fahrenheit, we follow the following steps:
- Multiply the temperature in Celsius by 9/5.
- Add 32 to the result obtained in step 1.
Let’s understand this formula with a few examples:
Example 1:
Suppose we have a temperature of 25 degrees Celsius. We can convert it to Fahrenheit using the formula:
F = (25 * 9/5) + 32
Calculating the above expression:
F = (45/5) + 32
F = 9 + 32
F = 41
Therefore, 25 degrees Celsius is equivalent to 41 degrees Fahrenheit.
Example 2:
Let’s take another example. Consider a temperature of 0 degrees Celsius. Using the formula, we have:
F = (0 * 9/5) + 32
Calculating the above expression:
F = 0 + 32
F = 32
Hence, 0 degrees Celsius is equal to 32 degrees Fahrenheit.
Example 3:
Now, let’s consider a negative temperature, say -10 degrees Celsius. Applying the formula:
F = (-10 * 9/5) + 32
Calculating the above expression:
F = (-90/5) + 32
F = -18 + 32
F = 14
Thus, -10 degrees Celsius is equal to 14 degrees Fahrenheit.
Also read: C Programming Examples
Convert Celsius to Fahrenheit using C Program
Now that we understand the formula and its usage, let’s write a C program to convert Celsius to Fahrenheit. Here’s the code:
#include <stdio.h>
int main() {
float celsius, fahrenheit;
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = (celsius * 9/5) + 32;
printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);
return 0;
}
Explanation of the Code
Let’s break down the code and explain each part:
- We start by including the necessary header file,
<stdio.h>
, which contains the functions for input and output operations. - Next, we define the
main
function, which is the entry point of our program. - Inside the
main
function, we declare two variables of typefloat
:celsius
andfahrenheit
. These variables will store the temperature values. - We use the
printf
function to display a prompt asking the user to enter the temperature in Celsius. - Then, we use the
scanf
function to read the input from the user and store it in thecelsius
variable. - Next, we apply the conversion formula
(celsius * 9/5) + 32
to calculate the temperature in Fahrenheit. The result is stored in thefahrenheit
variable. - Finally, we use the
printf
function to display the converted temperature in Fahrenheit. The format specifier%.2f
is used to display the result with two decimal places. - The program ends with the
return 0
statement, indicating successful execution.
Testing the Program
To test the program, you can compile and run it. Enter a temperature value in Celsius when prompted, and the program will display the converted temperature in Fahrenheit.
Here are a few examples of program execution:
Example 1:
Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77.00
Example 2:
Enter temperature in Celsius: 0
Temperature in Fahrenheit: 32.00
Example 3:
Enter temperature in Celsius: -10
Temperature in Fahrenheit: 14.00
Congratulations! You have successfully written a C program to convert Celsius to Fahrenheit.
Conclusion
In this article, we discussed how to write a C program to convert temperature from Celsius to Fahrenheit. We explained the conversion formula, provided a step-by-step explanation of the code, and included a complete code example.
Additionally, we demonstrated the formula’s usage with examples to further clarify the conversion process. Now you can use this program to convert temperatures in your own projects. Happy coding!
Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.