In: Computer Science
Temperature Converter
Modify the previous version of this program so that it uses a loop to display a range of temperature conversions for either Fahrenheit to Celsius or Celsius to Fahrenheit.
Note: You can start with the code from the previous version, then modify it slightly after it prompts the user for the direction to convert. It will then ask the user for a starting temperature and ending temperature. Assuming they entered the lower number first (if not, tell them and end the program), loop through the range provided, incrementing by 1 for each iteration of the loop, and generate the appropriate table.
The output should look like this -- user inputs are in bold blue type: Temperature Conversion Table Enter c (or C) to convert Fahrenheit to Celsius or f (or F) to convert Celsius to Fahrenheit: F Enter the starting temperature: 30 Enter the ending temperature: 42 Celsius Fahrenheit 30 86.0 31 87.8 32 89.6 33 91.4 34 93.2 35 95.0 36 96.8 37 98.6 38 100.4 39 102.2 40 104.0 41 105.8 42 107.6 Running the program again: Temperature Conversion Table Enter c (or C) to convert Fahrenheit to Celsius or f (or F) to convert Celsius to Fahrenheit: c Enter the starting temperature: -4 Enter the ending temperature: 4 Fahrenheit Celsius -4 -20.0 -3 -19.4 -2 -18.9 -1 -18.3 0 -17.8 1 -17.2 2 -16.7 3 -16.1 4 -15.6
As you have not mentioned the language hence I am using
C++
Below is the C++ code I hope that i have provided sufficient
comments for your better understanding Note that I have done proper
indentation but this code is automatically left alligned on this
interface
#include <stdio.h>
#include<iostream>
using namespace std;
int main()
{
char ch;
int startTemp;
int endTemp;
//Take user input
cout<<"Temperature Conversion Table"<<endl;
cout<<"Enter c (or C) to convert Fahrenheit to Celsius or f
(or F) to convert Celsius to Fahrenheit: ";
cin>>ch;
cout<<"Enter the starting temperature: ";
cin>>startTemp;
cout<<"Enter the ending temperature: ";
cin>>endTemp;
if(startTemp>endTemp) //invalid temperature range
{
cout<<"Starting temperature should be less than ending
temperature";
}
else
{
if(ch == 'F' || ch == 'f') //farheneit to celsius
{
cout<<"Celsius\tFahrenheit"<<endl;
for(int i=startTemp; i<=endTemp;i++)
{
double fahreneit = i*9.0/5 + 32;
cout<<i<<"\t"<<fahreneit<<endl;
}
}
else //celsius to farheneit
{
cout<<"Fahrenheit\tCelsius"<<endl;
for(int i=startTemp; i<=endTemp;i++)
{
double celsius = (i-32)*5.0/9;
cout<<i<<"\t\t"<<celsius<<endl;
}
}
}
return 0;
}
Below is the screenshot of output
I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any