In C++ The formula for converting a temperature from Fahrenheit to Celsius is C=
ID: 3864622 • Letter: I
Question
In C++
The formula for converting a temperature from Fahrenheit to Celsius is
C=5/9(f-32)
And the formula for converting from Celsius to Fahrenheit is
F=9/5*C + 32
Write a function named celsius that accepts a Fahrenheit temperature as an argument, and returns that temperature converted to Celsius. Write another function named fahrenheit that accepts a Celsius temperature as an argument, and returns that temperature converted to Fahrenheit.
Demonstrate the first function by calling it in a loop that display a table of Fahrenheit temperatures 0 through 20, and their Celsius equivalent. Demonstrate the second function by calling it in a loop that display a table of Celsius temperatures 0 through 20, and their Fahrenheit equivalent.
Explanation / Answer
#include <iostream>
using namespace std;
double celsius (double f){
return ((f - 32) * 5)/9;
}
double fahrenheit (double c){
return c*(9/5) + 32;
}
int main()
{
cout << "Fahrenheit temperatures: " << endl;
for(int i=0; i<=20; i++){
cout<<i <<" "<<celsius(i)<<endl;
}
cout << "Celsius temperatures: " << endl;
for(int i=0; i<=20; i++){
cout<<i <<" "<<fahrenheit(i)<<endl;
}
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Fahrenheit temperatures:
0 -17.7778
1 -17.2222
2 -16.6667
3 -16.1111
4 -15.5556
5 -15
6 -14.4444
7 -13.8889
8 -13.3333
9 -12.7778
10 -12.2222
11 -11.6667
12 -11.1111
13 -10.5556
14 -10
15 -9.44444
16 -8.88889
17 -8.33333
18 -7.77778
19 -7.22222
20 -6.66667
Celsius temperatures:
0 32
1 33
2 34
3 35
4 36
5 37
6 38
7 39
8 40
9 41
10 42
11 43
12 44
13 45
14 46
15 47
16 48
17 49
18 50
19 51
20 52