w3resource

C++ Exercises: Convert temperature in Fahrenheit to Celsius

C++ Basic: Exercise-21 with Solution

Write a C++ program to convert temperature in Fahrenheit to Celsius.

Sample Solution:

C++ Code :

#include <iostream> // Including the input-output stream header file

using namespace std; // Using the standard namespace

int main() // Start of the main function
{
    float frh, cel; // Declaring floating-point variables for Fahrenheit and Celsius temperatures

    cout << "\n\n Convert temperature in Fahrenheit to Celsius :\n"; // Outputting a message indicating temperature conversion
    cout << "---------------------------------------------------\n"; // Outputting a separator line

    cout << " Input the temperature in Fahrenheit : "; // Prompting the user to input the temperature in Fahrenheit
    cin >> frh; // Taking input for the Fahrenheit temperature from the user

    cel = ((frh * 5.0) - (5.0 * 32)) / 9; // Converting Fahrenheit to Celsius using the formula: (Fahrenheit - 32) * 5/9

    cout << " The temperature in Fahrenheit : " << frh << endl; // Displaying the input temperature in Fahrenheit
    cout << " The temperature in Celsius : " << cel << endl; // Displaying the converted temperature in Celsius
    cout << endl; // Outputting a blank line for better readability

    return 0; // Returning 0 to indicate successful program execution
} // End of the main function

Sample Output:

 Convert temperature in Fahrenheit to Celsius :                        
---------------------------------------------------                    
 Input the temperature in Fahrenheit : 95                              
 The temperature in Fahrenheit : 95                                    
 The temperature in Celsius : 35

Flowchart:

Flowchart: Convert temperature in Fahrenheit to Celsius

C++ Code Editor:

Previous: Write a program in C++ to convert temperature in Celsius to Fahrenheit.
Next: Write a program in C++ to find the third angle of a triangle.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/cpp-exercises/basic/cpp-basic-exercise-21.php