Here is a C++ program to convert Celsius to Fahrenheit and vice versa. First program converts Celsius to Fahrenheit, second program converts Fahrenheit to Celsius and we in third program we have used class for conversion.
C++ program to convert Celsius to Fahrenheit
#include<iostream>
using namespace std;
int main()
{
float celsius,fahrenheit;
cout<<"Enter the temperature in Celsius ";
cin>>celsius;
fahrenheit=celsius*9/5+32;
cout<<"Temperature in Fahrenheit is "<<fahrenheit<<endl;
return 0;
}
using namespace std;
int main()
{
float celsius,fahrenheit;
cout<<"Enter the temperature in Celsius ";
cin>>celsius;
fahrenheit=celsius*9/5+32;
cout<<"Temperature in Fahrenheit is "<<fahrenheit<<endl;
return 0;
}
OUTPUT
C++ program convert Fahrenheit to Celsius
#include<iostream>
using namespace std;
int main()
{
float celsius,fahrenheit;
cout<<"Enter the temperature in Fahrenheit ";
cin>>fahrenheit;
celsius=(fahrenheit-32)*5/9;
cout<<"Temperature in Celsius is "<<celsius<<endl;
return 0;
}
using namespace std;
int main()
{
float celsius,fahrenheit;
cout<<"Enter the temperature in Fahrenheit ";
cin>>fahrenheit;
celsius=(fahrenheit-32)*5/9;
cout<<"Temperature in Celsius is "<<celsius<<endl;
return 0;
}
OUTPUT
C++ program convert Fahrenheit to Celsius and vice versa using class
#include<iostream>
using namespace std;
class convert
{
private:
float celsius,fahrenheit;
public:
float celtofah()
{
fahrenheit=celsius*9/5+32;
return fahrenheit;
}
void getcelsius()
{
cout<<"Enter the temperature in Celsius : ";
cin>>celsius;
}
void getfahrenheit()
{
cout<<"Enter the temperature in Fahrenheit : ";
cin>>fahrenheit;
}
float fahtocel()
{
celsius=(fahrenheit-32)*5/9;
return celsius;
}
};
int main()
{
convert obj;
int choice;
float result;
cout<<"*****Menu*****\n1.) Convert Celsius to Fahrenheit\n2.) Convert Fahrenheit to Celsius\nEnter your choice : ";
cin>>choice;
switch(choice)
{
case 1 :
obj.getcelsius();
result=obj.celtofah();
cout<<"Temperature in Fahrenheit : "<<result;
break;
case 2:
obj.getfahrenheit();
result=obj.fahtocel();
cout<<"Temperature in Celsius : "<<result;
break;
default :
cout<<"Sorry wrong choice ";
}
return 0;
}
using namespace std;
class convert
{
private:
float celsius,fahrenheit;
public:
float celtofah()
{
fahrenheit=celsius*9/5+32;
return fahrenheit;
}
void getcelsius()
{
cout<<"Enter the temperature in Celsius : ";
cin>>celsius;
}
void getfahrenheit()
{
cout<<"Enter the temperature in Fahrenheit : ";
cin>>fahrenheit;
}
float fahtocel()
{
celsius=(fahrenheit-32)*5/9;
return celsius;
}
};
int main()
{
convert obj;
int choice;
float result;
cout<<"*****Menu*****\n1.) Convert Celsius to Fahrenheit\n2.) Convert Fahrenheit to Celsius\nEnter your choice : ";
cin>>choice;
switch(choice)
{
case 1 :
obj.getcelsius();
result=obj.celtofah();
cout<<"Temperature in Fahrenheit : "<<result;
break;
case 2:
obj.getfahrenheit();
result=obj.fahtocel();
cout<<"Temperature in Celsius : "<<result;
break;
default :
cout<<"Sorry wrong choice ";
}
return 0;
}
No comments:
Post a Comment