fahrenheit to celsius formula in c
Fahrenheit to Celsius Formula in C
Answer:
To convert temperatures from Fahrenheit to Celsius in the C programming language, you can implement a simple formula. The mathematical equation for converting Fahrenheit to Celsius is:
In C, you can represent this formula within a program as follows:
-
Include the necessary headers:
- For standard input and output operations, include the
<stdio.h>
library.
- For standard input and output operations, include the
-
Define the main function:
- In the
main
function, you can prompt the user to enter a temperature in Fahrenheit. - Then, apply the conversion formula to obtain the Celsius value.
- Finally, display the result.
- In the
Here is a complete example program for converting Fahrenheit to Celsius in C:
#include <stdio.h>
int main() {
float fahrenheit, celsius;
// Prompt the user to enter the temperature in Fahrenheit
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
// Convert Fahrenheit to Celsius using the formula
celsius = 5.0 / 9.0 * (fahrenheit - 32);
// Display the result
printf("%.2f Fahrenheit is equal to %.2f Celsius\n", fahrenheit, celsius);
return 0;
}
Explanation:
-
Headers Inclusion:
- The
<stdio.h>
library is included to useprintf
andscanf
functions.
- The
-
Variable Declaration:
float fahrenheit, celsius;
declares two variables to hold the Fahrenheit input and the calculated Celsius temperature.
-
User Input:
- The program uses
printf
to prompt the user to input the temperature in Fahrenheit. scanf("%f", &fahrenheit);
reads the user input and stores it in thefahrenheit
variable.
- The program uses
-
Conversion Formula:
- The conversion is performed with the formula
celsius = 5.0 / 9.0 * (fahrenheit - 32);
. Note the use of5.0
and9.0
to ensure floating-point division is performed.
- The conversion is performed with the formula
-
Output:
- The
printf
function prints the result, showing the converted Celsius temperature with two decimal places for precision.
- The
Final Answer:
By running this program, you can convert any temperature from Fahrenheit to Celsius. For example, if the user inputs 100
Fahrenheit, the output would be:
100.00 Fahrenheit is equal to 37.78 Celsius
This code effectively demonstrates how to perform temperature conversion in the C programming language using the Fahrenheit to Celsius formula.