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';
}
}
Leave a reply