Fahrenheit to celsius formula in c

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:

C = \frac{5}{9} \times (F - 32)

In C, you can represent this formula within a program as follows:

  1. Include the necessary headers:

    • For standard input and output operations, include the <stdio.h> library.
  2. 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.

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:

  1. Headers Inclusion:

    • The <stdio.h> library is included to use printf and scanf functions.
  2. Variable Declaration:

    • float fahrenheit, celsius; declares two variables to hold the Fahrenheit input and the calculated Celsius temperature.
  3. 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 the fahrenheit variable.
  4. Conversion Formula:

    • The conversion is performed with the formula celsius = 5.0 / 9.0 * (fahrenheit - 32);. Note the use of 5.0 and 9.0 to ensure floating-point division is performed.
  5. Output:

    • The printf function prints the result, showing the converted Celsius temperature with two decimal places for precision.

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.