Answer to Question #172179 in C++ for Fatimah

Question #172179

Write a program in C++ which asks user for temperature as well as scale and displays converted temperature.


1
Expert's answer
2021-03-16T10:44:03-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS