Convert Fahrenheit to Celsius

Beginner-friendly C++ program that converts temperature from Fahrenheit to Celsius using the standard formula and formats the result.

BeginnerTopic: Basic Programs
Back

C++ Convert Fahrenheit to Celsius Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
                  #include <iomanip>
                  using namespace std;
                  
                  int main() {
                      float fahrenheit, celsius;
                      
                      cout << "Enter temperature in Fahrenheit: ";
                      cin >> fahrenheit;
                      
                      // Formula: C = (F - 32) * 5/9
                      celsius = (fahrenheit - 32) * 5.0 / 9.0;
                      
                      cout << fixed << setprecision(2);
                      cout << fahrenheit << "°F = " << celsius << "°C" << endl;
                      
                      return 0;
                  }
Output
Enter temperature in Fahrenheit: 98.6
                  98.60°F = 37.00°C

Understanding Convert Fahrenheit to Celsius

This program helps you convert a temperature from Fahrenheit to Celsius. It uses a well-known formula from science and applies it using simple arithmetic in C++. The program also shows how to format decimal numbers so they look neat and easy to read.

---

1. Why Convert Fahrenheit to Celsius?

Temperature is measured differently in different places:

The United States mostly uses Fahrenheit (°F)
Most other countries use Celsius (°C)

This program teaches how to convert between the two using math.

---

2. Header Files

The program uses two headers:

1.#include <iostream>

Allows us to use cin (input) and cout (output).

2.#include <iomanip>

Provides tools like fixed and setprecision() to control how decimal numbers are printed.

---

3. Declaring Float Variables

We use:

float fahrenheit, celsius;

fahrenheit → stores the temperature entered by the user
celsius → stores the converted temperature

We use float because temperature often includes decimal values (e.g., 98.6°F).

---

4. Taking Input From the User

The program displays:

cin >> fahrenheit;

Whatever the user types (like 98.6) is stored in the variable fahrenheit.

---

                  cout << "Enter temperature in Fahrenheit: ";

5. The Conversion Formula

The conversion formula is:

C = (F - 32) × 5/9

This formula converts a temperature from the Fahrenheit scale to the Celsius scale.

In the program:

celsius = (fahrenheit - 32) * 5.0 / 9.0;

Here's why we use

5.0 / 9.0

instead of 5 / 9:

5 / 9 (without decimal) would perform integer division → result = 0
5.0 / 9.0 performs floating-point division → correct result ≈ 0.5555

So 5.0 and 9.0 ensure accurate decimal calculations.

---

6. Formatting the Decimal Output

Before printing the final result, the program uses:

This means:

fixed → show numbers in normal decimal form
setprecision(2) → show exactly 2 decimal places

So:

98.6 becomes 98.60
37 becomes 37.00

This makes the output clean and professional.

---

                  cout << fixed << setprecision(2);

7. Displaying the Result

The output is printed as:

If the user enters 98.6, the program prints:

98.60°F = 37.00°C

---

                  cout << fahrenheit << "°F = " << celsius << "°C" << endl;

8. return 0;

This ends the program successfully.

---

Summary

The program reads a temperature in Fahrenheit.
It converts it to Celsius using the formula C = (F - 32) × 5/9.
Floating-point numbers ensure accurate calculations.
setprecision(2) formats the output to two decimal places.
This is a simple and practical program used in many real-world applications.

This example helps build confidence with arithmetic, formulas, and formatted output in C++.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for Convert Fahrenheit to Celsius

This C++ program is part of the "Basic Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents