Gold University of Minnesota M. Skip to main content.University of Minnesota. Home page.
 
 
 

What's inside.

Ta Email

Download Compiler

Final Project

Lab Notes

Office Hours

Schedule

Syllabus

Announcements

Check Grades

 

CSci 1113 Home

 
 

Printer-friendly version

 
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
 
The University of Minnesota is an equal opportunity educator and employer.
CSci 1113: C++ Programming