|
|
|
Temperature Conversion Program: if-else Statement
// Temperature Conversion Program
#include <iostream>
using namespace std;
int main()
{
char temp_type;
double temp, fahren, celsius;
cout << "Enter the temperature to be converted: ";
cin >> temp;
cout << "Enter f if temperature is in fahrenheit";
cout << "\nor c if in Celsius: ";
cin >> temp_type;
// set output formats
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
if(temp_type == 'f')
{
celsius = (5.0 / 9.0) * (temp - 32.0);
cout << "\nThe equivalent Celsius temperature
is "
<< celsius
<< endl;
}
else
{
fahren = (9.0 / 5.0) * temp + 32.0;
cout << "\nThe equivalent Fahrenheit
temperature is "
<< fahren
<< endl;
}
return 0;
}
% g++ temp2.cpp
% a.out
Enter the temperature to be converted: 20
Enter f if temperature is in fahrenheit
or c if in Celsius: c
The equivalent Fahrenheit temperature is 68.00
% a.out
Enter the temperature to be converted: 98.6
Enter f if temperature is in fahrenheit
or c if in Celsius: f
The equivalent Celsius temperature is 37.00
|
|