Table of Content
Converting Fahrenheit to Celsius is a common mathematical operation, especially when working with temperature conversions. The formula for converting temperature from Fahrenheit (°F) to Celsius (°C) is simple:
°C = 5/9 x (°F-32)
This formula subtracts 32 from the Fahrenheit value and then multiplies the result by 5/9 to convert it into Celsius. Let's break down how you can implement this formula in a C program.
Program Explanation
In the C programming language, we can write a program that takes the temperature in Fahrenheit as input from the user, converts it to Celsius using the formula, and displays the result. Below is a simple C program to achieve this.
C Program to Convert Fahrenheit to Celsius
#include <stdio.h>
int main() {
float fahrenheit, celsius;
// Asking for user input
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
// Conversion formula
celsius = (5.0 / 9.0) * (fahrenheit - 32);
// Output the result
printf("%.2f Fahrenheit is equivalent to %.2f Celsius.\n", fahrenheit, celsius);
return 0;
}
Program Breakdown
Include the Standard Input-Output Library: We include stdio.h
to allow the program to use the printf()
and scanf()
functions for input and output.
Declare Variables: Two floating-point variables fahrenheit
and celsius
are declared to hold the temperature values.
User Input: The scanf()
function is used to read the Fahrenheit temperature from the user.
Apply the Conversion Formula: The formula (5.0 / 9.0) * (fahrenheit - 32)
is used to convert the Fahrenheit temperature to Celsius. It's essential to use 5.0 / 9.0
instead of 5 / 9
to ensure the division is performed as a floating-point operation, not as integer division.
Display the Result: The printf()
function is used to display the temperature in both Fahrenheit and Celsius.
Example
If the user inputs 98.6
Fahrenheit, the program will perform the following steps:
- Subtract 32 from 98.6, which gives 66.6.
- Multiply 66.6 by 5/9 (approximately 0.5556), resulting in 37°C.
The output would be:
98.60 Fahrenheit is equivalent to 37.00 Celsius.
Conclusion
This C program provides a simple and effective way to convert Fahrenheit to Celsius. By taking advantage of basic user input, arithmetic operations, and output functions, this program helps demonstrate how temperature conversions are easily done with C.
Discussion