Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Converting Fahrenheit to Celsius using C++ programming

Write a program that converts integer Fahrenheit temperatures
from 0 to 212 degrees to floating-point Celsius temperatures with 3 digits of precision. Use
the formula
celsius = 5.0 / 9.0 * ( fahrenheit – 32 );
to perform the calculation. The output should be printed in two right-justified columns and the
Celsius temperatures should be preceded by a sign for both positive and negative values.

Answer:



#include <iostream> 
#include <iomanip> 
using namespace std;

int main()
{
   double celsius; // holds celsius temperature

   // create column headings with fields of length 20
   cout << setw( 20 ) << "Fahrenheit " << setw( 20 ) << "Celsius\n"
      << fixed << showpoint;

   // convert fahrenheit to celsius and display temperatures
   // showing the sign for celsius temperatures
   for ( int fahrenheit = 0; fahrenheit <= 212; ++fahrenheit ) 
   {
       celsius = 5.0 / 9.0 * ( fahrenheit - 32 );
       cout << setw( 15 ) << noshowpos << fahrenheit << setw( 23 ) 
          << setprecision( 3 ) << showpos << celsius << '\n';
   } 
} 

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!