Write a C++ program that accepts a number followed by one space and then a lette
ID: 3542689 • Letter: W
Question
Write a C++ program that accepts a number followed by one space and then a letter. If the letter following the number is f or F, the program is to treat the number entered as a temperature in degrees Fahrenheit, convert the number to the equivalent degrees Celsius, and print a suitable display message. If the letter following the number is c or C, the program is to consider the number entered as a Celsius temperature, convert the number to the equivalent degrees in Fahrenheit, and print a suitable display message. while the letter is neither f (F) nor c (C) the program is to print a message that the data entered is incorrect and terminate. The conversion formulas:
celsius = (5.0/9.0) * (fahrenheit - 32.0)
fahrenheit = (9.0/5.0) * celsius + 32.0
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
int num;
char ch;
cout << "Enter a number followed by space and a letter :";
cin >> num >> ch;
cout << endl;
if(ch=='f' || ch=='F')
{
cout << "for given " << num << " corresponding Celsius temperature is " << (5.0/9.0) * (num - 32.0) <<endl;
}
else if(ch=='c' || ch=='C')
{
cout << "for given " << num << " corresponding fahrenheit temperature is " << ((9.0/5.0) * num + 32.0) <<endl;
}
else
{
cout << "Incorrect data entered. so terminating the program " << endl;
}
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.