C++ program for Celsius to Fahrenheit and Fahrenheit to Celsius conversion

Introduction :

In this tutorial, we will learn how to convert a Celsius temperature value to Fahrenheit and vice versa. The program will take the inputs from the user and print out the result.

We are using the following two formulae :

fahrenheit = (1.8 * celsius) + 32

celsius = (fahrenheit - 32) / 32

The first one is used to convert a Celsius value to Fahrenheit and the second one is used to convert a Fahrenheit to Celsius.

The program will also ask the user to choose the conversion type at first i.e. Fahrenheit to Celsius or Celsius to Fahrenheit. Let’s take a look at the program :

C++ program of temperature conversion :

#include <iostream>
using namespace std;

int main()
    float fahrenheit, celcius;
    int choice;

    cout << "Enter 1 : Fahrenheit to celcius \nEnter 2 : Celcius to Fahrenheit" << endl; cin >> choice;

    if (choice == 1)
        cout << "Enter the Fahrenheit value :" << endl; cin >> fahrenheit;

        celcius = (fahrenheit - 32) / 1.8;
        cout << "It is " << celcius << " degree celcius" << endl;
    else if (choice == 2)
        cout << "Enter the Celcius value :" << endl; cin >> celcius;

        fahrenheit = (1.8 * celcius) + 32;
        cout << "It is " << fahrenheit << " degree Fahrenheit" << endl;
        cout << "Please enter a valid input" << endl;
    return 0;

c plus plus Fahrenheit to celsius conversion and vice versa

Explanation :

The commented numbers in the above program denote the step numbers below :

  1. main() is the starting method of a C++ program. This method is called first.
  2. We are creating two floating point values to store the Fahrenheit and Celsius values. choice is an integer variable to store the option selected by the user.
  3. Ask the user to enter 1 for Fahrenheit to Celsius and 2 for Celsius to Fahrenheit conversion. Read the user input value and store it in the choice variable.
  4. Check the value of choice using an if-else if-else condition.
  5. If the value of choice is 1, the conversion is for Fahrenheit to Celsius. Ask the user to enter the Fahrenheit value, store it in Fahrenheit variable and calculate the Celsius using the same formula we have seen above. Finally, print out the result to the user.
  6. Similar to the above step, do the conversion from Celsius to Fahrenheit.
  7. If the input is not valid or if it is not either 1 or 2, print a message to the user.

Sample Output :

Enter 1 : Fahrenheit to celcius
Enter 2 : Celcius to Fahrenheit
Enter the Fahrenheit value :
It is 16.6667 degree celcius

Enter 1 : Fahrenheit to celcius
Enter 2 : Celcius to Fahrenheit
Enter the Celcius value :
It is 62.0001 degree Fahrenheit

Conclusion :

In this tutorial, we have learned how to convert a temperature in C++. Try to go through the program and drop one comment below if you have any queries.

Similar tutorials :

Where is the color and why codevscolor ?

Long story short, I love paintings and I paint on weekends. We(me and my wife) have one Youtube channel. Below is a video that I did recently. If you love this please do subscribe to support us 😊