Jump to content

Programming Fundamentals/Function Examples C++

From Wikibooks, open books for an open world

Temperature

[edit | edit source]
 // This program asks the user for a Fahrenheit temperature, 
 // converts the given temperature to Celsius,
 // and displays the results.
 //
 // References:
 // https://www.mathsisfun.com/temperature-conversion.html
 // https://en.wikibooks.org/wiki/C%2B%2B_Programming
 
 #include <iostream>
 
 using namespace std;
 
 double getFahrenheit();
 double calculateCelsius(double);
 void displayResult(double, double);
 
 int main() {
     double fahrenheit;
     double celsius;
     
     fahrenheit = getFahrenheit();
     celsius = calculateCelsius(fahrenheit);
     displayResult(fahrenheit, celsius);
     
     return 0;
 }
 
 double getFahrenheit() {
     double fahrenheit;
     
     cout << "Enter Fahrenheit temperature:" << endl; 
     cin >> fahrenheit;
     
     return fahrenheit;
 }
 
 double calculateCelsius(double fahrenheit) {
     double celsius;
     
     celsius = (fahrenheit - 32) * 5 / 9;
     
     return celsius;
 }
 
 void displayResult(double fahrenheit, double celsius) {
     cout << fahrenheit << "° Fahrenheit is " 
         << celsius << "° Celsius" << endl;
 }

Output

[edit | edit source]
Enter Fahrenheit temperature:
 100
100° Fahrenheit is 37.7778° Celsius

References

[edit | edit source]