Fahrenheit To Celsius Conversion in C using If-else

In this tutorial, you will learn how to write a program to convert temperature from Fahrenheit to Celsius using the if-else statement. This post suits topics like Convert Temperature From Fahrenheit To Celsius using if-else in C Programming.

Problem Statement

The problem is to write a C program that will convert the given temperature in Fahrenheit to Celsius. The user will give input the temperature in Fahrenheit and then return the output in Celsius temperature.

Example

Enter temperature in Fahrenheit : 77
Output: 25.00 Celsius

Algorithm To Convert Fahrenheit To Celsius

  1. Start the program.
  2. Declare variables fahrenheit and celsius of type float.
  3. Prompt the user to enter the temperature in Fahrenheit.
  4. Read the input temperature from the user and store it in the fahrenheit variable using scanf().
  5. Use an if-else statement to check if the entered temperature is below absolute zero (-459.67 Fahrenheit). If true, print an error message stating that the temperature is invalid.
  6. If the entered temperature is valid (not below absolute zero), perform the temperature conversion from Fahrenheit to Celsius using the formula (fahrenheit - 32) * 5 / 9 and store the result in the celsius variable.
  7. Print the original temperature in Fahrenheit and the converted temperature in Celsius using printf().
  8. End the program.

C Program to Convert the temperature from Fahrenheit to Celsius

#include <stdio.h>
int main() {
    float fahrenheit, celsius;
    printf("Enter temperature in Fahrenheit: ");
    scanf("%f", &fahrenheit);
    if (fahrenheit < -459.67) {
        printf("Invalid temperature: Absolute zero cannot be reached.\n");
    }
    else {
        celsius = (fahrenheit - 32) * 5 / 9;
        printf("%.2f Fahrenheit = %.2f Celsius\n", fahrenheit, celsius);
    }
    return 0;
}

Output 2

Enter temperature in Fahrenheit: 77
77.00 Fahrenheit = 25.00 Celsius

Program Explanation

  • The program starts by including the necessary header file stdio.h.
  • The main() function is defined.
  • Inside the main() function, two variables fahrenheit and celsius of type float are declared to store the input temperature in Fahrenheit and the converted temperature in Celsius, respectively.
  • The program prompts the user to enter the temperature in Fahrenheit by printing the message “Enter temperature in Fahrenheit: “.
  • The scanf() function is used to read a floating-point number from the user, and the input is stored in the fahrenheit variable using the & operator.
  • The program uses an if-else statement to check if the entered temperature is valid. The condition fahrenheit < -459.67 checks if the temperature is below absolute zero (-459.67 Fahrenheit). If the condition is true, it prints the message “Invalid temperature: Absolute zero cannot be reached.”
  • If the entered temperature is valid (not below absolute zero), the program performs the temperature conversion from Fahrenheit to Celsius using the formula (fahrenheit - 32) * 5 / 9. The result is stored in the celsius variable.
  • The program prints the original temperature in Fahrenheit and the converted temperature in Celsius using the printf() function. The format specifier %.2f is used to display the floating-point numbers with two decimal places.
  • The return 0 statement ends the program.

Conclusion

This C program allows the user to select a temperature conversion type (Fahrenheit to Celsius) and performs the conversion using if-else statements. It covers all possible cases and provides the appropriate output based on the user’s choice and input temperature.