C++ Program to Convert Temperature from Fahrenheit to Celsius

Temperature conversion is a common problem in both daily life and programming. One of the most frequent conversions is between Fahrenheit and Celsius. The formula to convert Fahrenheit to Celsius is given by

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

where C is the temperature in Celsius and Fis the temperature in Fahrenheit.

In this article, we will write a C++ program that takes a temperature in Fahrenheit as input and converts it to Celsius using the above formula.

Prerequisites

Before we begin, ensure you are familiar with:

  • Basic input and output operations in C++
  • Arithmetic operations in C++
  • Compiling and running a C++ program

C++ Program to Convert Temperature from Fahrenheit to Celsius

C++
#include <iostream>
using namespace std;

int main() {
    // Variable to store temperature in Fahrenheit
    double fahrenheit;
    
    // Prompt the user to enter temperature in Fahrenheit
    cout << "Enter temperature in Fahrenheit: ";
    cin >> fahrenheit;
    
    // Convert Fahrenheit to Celsius
    double celsius = (5.0 / 9.0) * (fahrenheit - 32);
    
    // Display the temperature in Celsius
    cout << "Temperature in Celsius: " << celsius << "°C" << endl;
    
    return 0;
}

Output

Example 1

C++
Enter temperature in Fahrenheit: 98.6
Temperature in Celsius: 37°C

Example 2

C++
Enter temperature in Fahrenheit: 32
Temperature in Celsius: 0°C

Example 3

C++
Enter temperature in Fahrenheit: 212
Temperature in Celsius: 100°C

Explanation:

  1. Variable Declaration:
    • fahrenheit is declared to store the input temperature in Fahrenheit.
    • celsius is declared to store the converted temperature in Celsius.
  2. Input Handling:
    • The program prompts the user to enter a temperature in Fahrenheit.
    • The input is read and stored in the fahrenheit variable.
  3. Conversion Formula:
    • The formula to convert Fahrenheit to Celsius is applied: C=59×(F−32)C = \frac{5}{9} \times (F – 32)C=95​×(F−32).
    • The result is stored in the celsius variable.
  4. Output:
    • The program displays the temperature in Celsius.

Conclusion

Converting temperature from Fahrenheit to Celsius is a straightforward task that can be easily implemented in C++. This program demonstrates how to perform the conversion using basic arithmetic operations and handle user input/output.