fahrenheit to celcius
To convert Fahrenheit to Celsius in the C programming language, you can use the following formula:
Celsius = (Fahrenheit - 32) * 5/9
Here's an example code snippet that demonstrates the conversion:
#include <stdio.h>
int main() {
float fahrenheit, celsius;
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 5 / 9;
printf("Temperature in Celsius: %.2f\n", celsius);
return 0;
}
Let's break down the code and explain each step:
- We include the
stdio.h
header file, which allows us to use theprintf
andscanf
functions for input and output. - In the
main
function, we declare two variables:fahrenheit
andcelsius
. These variables will hold the temperature values. - We prompt the user to enter the temperature in Fahrenheit using the
printf
function. - We read the input temperature from the user using the
scanf
function and store it in thefahrenheit
variable. - We apply the conversion formula
(Fahrenheit - 32) * 5/9
to calculate the temperature in Celsius and store it in thecelsius
variable. - Finally, we use the
printf
function to display the temperature in Celsius with two decimal places.
That's it! The code takes the temperature in Fahrenheit as input, converts it to Celsius using the formula, and then displays the result.