Q:

# C++ Program To Convert Celsius To Fahrenheit And Vice Versa Using Switch Case

Logic:-

For Converting Temperature Celsius to Fahrenheit, we are using a given Formula. We have just take a value or temperature in Celsius or Fahrenheit by the user and put the values in given formula and print the outcome given by formula on screen. Here in this problem we are given three option to the user and user have to choose any one of the first choices is Celsius To Fahrenheit, the second choice is Fahrenheit To Celsius and the Last one is Exit without testing any one of the queries.

Formula's

Celsius To Fahrenheit:-

Fahrenheit=(Celsius*9/5)+32;

Fahrenheit To Celsius:-

Celsius=(Fahrenheit-32)*5/9;

using if else instead of switch statement:

``````#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"1. For Celsius To Fahrenheit. \n";
cout<<"2. For Fahrenheit To Celsius. \n";
cout<<"3. For Exit\n\n";
cin>>a;

double cel,feh;
if(a==1)
{
cout<<"Enter The Temperature In Celsius\n";
cin>>cel;
feh=(cel*9/5)+32;
cout<<"\nTemperature In Fahrenheit Is = "<<feh ;
}
else if(a==2)
{
cout<<"Enter The Temperature In Fahrenheit\n";
cin>>feh;
cel=(feh-32)*5/9;
cout<<"\nTemperature In Celsius Is = "<<cel ;
}
else if(a==3)
exit(0);
else
cout<<"\nEnter The Right Choice \n";
}``````

``````#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"1. For Celsius To Fahrenheit. \n";
cout<<"2. For Fahrenheit To Celsius. \n";
cout<<"3. For Exit\n\n";
cin>>a;
switch(a)
{
double cel,feh;
case 1: cout<<"Enter The Temperature In Celsius\n";
cin>>cel;
feh=(cel*9/5)+32;
cout<<"\nTemperature In Fahrenheit Is = "<<feh ;
break;

case 2: cout<<"Enter The Temperature In Fahrenheit\n";
cin>>feh;
cel=(feh-32)*5/9;
cout<<"\nTemperature In Celsius Is = "<<cel ;
break;

case 3:exit(0);

default:cout<<"\nEnter The Right Choice \n";
break;
}
}
``````

Output:

1. For Celsius To Fahrenheit.

2. For Fahrenheit To Celsius.

3. For Exit

1

Enter The Temperature In Celsius

37

Temperature In Fahrenheit Is = 98.6