In this tutorial, we will learn the conversion of a temperature from Fahrenheit to Celsius (or Centigrade) in C++. Let us start by understanding the formula for the conversion of Fahrenheit into Celsius.
Fahrenheit and Celsius, both are the unit for measuring the temperature. Fahrenheit is represented by oF and Celsius by oC.
Formula to convert Fahrenheit into Celsius
celsius = (fahrenheit – 32)*5/9
We will write two programs for the conversion of Fahrenheit to Celsius in C++.
- Without the use of Function
- With the use of function
Fahrenheit to Celsius in C++
The program simply asks the user for the Fahrenheit temperature value in order to convert temperature from Fahrenheit to Celsius in C++. Then converts into its equivalent Celsius value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <iostream> using namespace std; int main() { float f, c; cout << "Enter the Fahrenheit value: "; cin >> f; // conversion calculation c = (f - 32) * 5/9; cout << "Equivalent Celsius value: " << c; return 0; } |
Output:
Enter the Fahrenheit value: 64
Equivalent Celsius value: 17.7778
C++ Program to convert Fahrenheit to Celsius using function
Here, we create a separate user-defined function in C++ to convert Fahrenheit to Celsius. The value of Fahrenheit entered by the user is passed to a function as an argument. The function returns the value after the calculation of the celsius value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <iostream> using namespace std; // conversion function float conversion(float f) { float c; c = (f - 32) * 5/9; return c; } int main() { float f, c; cout << "Enter the Fahrenheit value: "; cin >> f; // calling function c = conversion(f); cout << "Equivalent Celsius value: " << c; return 0; } |
Output:
Enter the Fahrenheit value: 80
Equivalent Celsius value: 26.6667