This blog is about providing theory as well as simple executable codes of different programming languages such as java, C, C++, and web programming, etc. This blog will be helpful to the IT students to learn about programming.

Tuesday, March 14, 2023

Write a C++ program that will ask for a temperature in Fahrenheit and display it in Celsius.

 here's an example program that asks the user for a temperature in Fahrenheit and displays it in Celsius:

#include <iostream>
using namespace std;
int main() {
    double fahrenheit, celsius;
    
    // get temperature in Fahrenheit from user
    cout << "Enter temperature in Fahrenheit: ";
    cin >> fahrenheit;
    
    // convert Fahrenheit to Celsius
    celsius = (fahrenheit - 32) * 5 / 9;
    
    // display temperature in Celsius
    cout << "Temperature in Celsius: " << celsius << endl;
    
    return 0;
}

Example output:

Enter temperature in Fahrenheit: 68
Temperature in Celsius: 20

In this program, we first declare two variables fahrenheit and celsius of type double to store the temperature in Fahrenheit and Celsius, respectively.

We then prompt the user to enter the temperature in Fahrenheit using cout and read in the value using cin.

Next, we convert the Fahrenheit temperature to Celsius using the formula (F - 32) * 5 / 9, where F is the temperature in Fahrenheit.

Finally, we display the temperature in Celsius using cout.

Note that we used the formula for converting Fahrenheit to Celsius, which is subtracting 32 from Fahrenheit and multiplying by 5/9.

No comments:

Post a Comment

If you have any doubts, please let me know

Slider Widget