here's an example program that asks the user for a temperature in Fahrenheit and displays it in Celsius:
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:
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