Cpp - Write program to create function to convert Fahrenheit to Celsius

Requirements

Write program to create function to convert Fahrenheit to Celsius

Create a convert() function that takes one argument: a float value called fahrenheit.

The converted value is returned by the function.

Hint

The three-step formula for converting Fahrenheit to Celsius:

  • Subtract 32 from the number.
  • Multiply the result by 5.
  • Divide that result by 9.

Demo

#include <iostream> 
   //from  w  w w. j  a va 2  s .  com
float convert(float); 
   
int main() 
{ 
    float fahrenheit; 
    float celsius; 
   
    std::cout << "Please enter the temperature in Fahrenheit: "; 
    std::cin >> fahrenheit; 
    celsius = convert(fahrenheit); 
    std::cout << "\nHere's the temperature in Celsius: "; 
    std::cout << celsius << "\n"; 
    return 0; 
} 
   
// function to convert Fahrenheit to Celsius 
float convert(float fahrenheit) 
{ 
    float celsius; 
    celsius = ((fahrenheit - 32) * 5) / 9; 
    return celsius; 
}

Result

Related Exercise