Write a program in C++ which asks user for temperature as well as scale and displays converted temperature.
#include<iostream>
#include<string>
using namespace std;
//The start point of the program
int main (){
//get temperature from the user
float temperature;
cout<<"Ente the temperature: ";
cin>>temperature;
//get scale from the user
string scale="";
while(scale.compare("C")!=0 && scale.compare("c")!=0 &&
scale.compare("F")!=0 && scale.compare("f")!=0 &&
scale.compare("K")!=0 && scale.compare("k")!=0){
cout<<"Select the scale (C,F or K): ";
cin>>scale;
}
float Celsius=temperature;
float Fahrenheit=temperature;
float Kelvin=temperature;
if (scale.compare("C")==0 || scale.compare("c")==0){
//Calculate Kelvin temperature
Kelvin=Celsius+273.15;
//Calculate Fahrenheit temperature
Fahrenheit=(Celsius * 1.8) + 32;
}else if (scale.compare("F")==0 || scale.compare("f")==0){
//Calculate Kelvin temperature
Kelvin=(Fahrenheit - 32) * 5/9 + 273.15;
//Calculate Celsius temperature
Celsius=(Fahrenheit - 32) * 5.0 / 9.0;
}else if (scale.compare("K")==0 || scale.compare("k")==0){
//Calculate Fahrenheit temperature
Fahrenheit=(Kelvin - 273.15) * 9/5 + 32;
//Calculate Celsius temperature
Celsius= Kelvin - 273.15;
}
//Dplay converted temperature.
printf("\nCelsius = %.2fC",Celsius);
printf("\nFahrenheit = %.2fF",Fahrenheit);
printf("\nKelvin = %.2fK",Kelvin);
cout<<"\n\n\n";
system("pause");
return 0;
}
Comments
Leave a comment