How to Convert Celsius to Fahrenheit in C
In this post, we’ll learn how to convert a temperature from Celsius to Fahrenheit using a simple C program. The conversion formula we’ll use is:
fahrenheit = (celsius × 9 / 5) + 32
C Program Code
#include <stdio.h>
int main() {
float celsius, fahrenheit;
// Prompt the user to enter the temperature in Celsius
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
// Convert Celsius to Fahrenheit
fahrenheit = (celsius * 9 / 5) + 32;
// Display the result
printf("%.2f Celsius is equal to %.2f Fahrenheit\n", celsius, fahrenheit);
return 0;
}
How the Program Works
- We include the standard input/output library
<stdio.h>
. - We declare two
float
variables:celsius
andfahrenheit
. - We prompt the user to enter the Celsius temperature using
printf()
and read it withscanf()
. - We perform the conversion using the formula fahrenheit = (celsius × 9 / 5) + 32.
- We display the converted temperature using
printf()
with two decimal precision.
Example Output
Here’s what the output might look like:
Enter temperature in Celsius: 25
25.00 Celsius is equal to 77.00 Fahrenheit
Conclusion
This simple program illustrates how to convert a Celsius temperature to Fahrenheit in C. You can enhance this by adding error handling or expanding it to handle multiple conversions in a loop.