In this tutorial, we will learn the conversion of a temperature from Celsius to Fahrenheit 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 Celsius to Fahrenheit
fahrenheit
= ((celsius * 9/5) + 32)
We will write two programs for the conversion of Celsius to Fahrenheit in C++.
- Without the use of Function
- With the use of function
Celsius to Fahrenheit in C++
The program simply asks the user for the Celsius temperature value in order to convert temperature from Celsius to Fahrenheit in C++. Then converts into its equivalent Fahrenheit 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 Celsius value: "; cin >> c; // conversion calculation f = (c * 9/5) + 32; cout << "Equivalent Fahrenheit value: " << f; return 0; } |
Output:
Enter the Celsius value: 35
Equivalent Fahrenheit value: 95
C++ Program to convert Celsius to Fahrenheit using function
Here, we create a separate user-defined function in C++ to convert Celsius to Fahrenheit. The value of Celsius entered by the user is passed to a function as an argument. The function returns the value after the calculation of the Fahrenheit 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 c) { float f; f = (c * 9/5) + 32; return f; } int main() { float c, result; cout << "Enter the Celsius value: "; cin >> c; // calling function result = conversion(c); cout << "Equivalent Fahrenheit value: " << result; return 0; } |
Output:
Enter the Celsius value: 35
Equivalent Fahrenheit value: 95