C++ program for temperature conversion

In this article, you will learn a C++ program for temperature conversion. The user can select and enter any of the temperatures from Celsius, Fahrenheit, or Kelvin and the program displays its corresponding temperatures on the console screen.

Formula

Here are the formulas for temperature conversion.

Celsius to Fahrenheit° F = 9/5 ( ° C) + 32
Celsius to KelvinK = ° C + 273
Fahrenheit to Celsius° C = 5/9 (° F – 32)
Fahrenheit to KelvinK = 5/9 (° F – 32) + 273
Kelvin to Celsius° C = K – 273
Kelvin to Fahrenheit° F = 9/5 (K – 273) + 32

Example: Temperature conversion in C++

#include<iostream>
using namespace std;

int main()
{
   float fahr, cel, kel;
   char option;

   cout << "Choose One Option:" << endl;
   cout << "1: For Celsius" << endl;
   cout << "2: For Fahrenheit" << endl;
   cout << "3: For Kelvin" << endl;
   cin >> option;

   if (option == '1')
   {
      cout << "Enter the temperature in Celsius: ";
      cin >> cel;

      fahr = (1.8 * cel) + 32.0; 
      kel =  cel + 273;
	  
      cout << "\nTemperature in  Fahrenheit: " << fahr << " F" << endl;
      cout << "\nTemperature in  Kelvin: " << kel << " K" << endl;
   }
   else if (option == '2')
   {
      cout << "Enter the temperature in Fahrenheit: ";
      cin >> fahr;

      cel = (fahr - 32) / 1.8;
	  kel = 0.55 * (fahr - 32) + 273;

      cout << "\nTemperature in  Celsius: " << cel << " C" << endl;
      cout << "\nTemperature in  Kelvin: " << kel << " K" << endl;
   }
    else if (option == '3')
   {
      cout << "Enter the temperature in Kelvin: ";
      cin >> fahr;

      cel = kel - 273;
	  fahr = 1.8 * (kel - 273) + 32;

      cout << "\nTemperature in  Celsius: " << cel << " C" << endl;
      cout << "\nTemperature in  Fahrenheit: " << fahr << " F" << endl;
   }
   else
      cout << "Wrong Input." << endl;

   return 0;
}

Output

cpp program for temperature conversion


Recommended Articles

C++ program to reverse numbers

C++ program to print star patterns

Scroll to Top