0
Celsius and Fahrenheit conversion
Write a program that has Main() call a user-defined function that takes Celsius temp value as an argument & then returns the equivalent Fahrenheit value. The program should request the Celsius value as input from the user and display the result as: Enter a Celsius value: 20 20 degrees Celsius is 68 degrees Fahrenheit.
2 odpowiedzi
+ 1
#include <iostream>
using namespace std;
int tempF(int c){
int f;
f = ((c * 9)/5)+32;
return f;
}
int main(){
int tempC;
cout << "Enter a Celsius value: ";
cin >> tempC;
cout << tempC << " degrees Celsius is " << tempF(tempC) << " degrees Fahrenheit." << endl;
return 0;
}
+ 1
°F to °C Deduct 32, then multiply by 5, then divide by 9
°C to °F Multiply by 9, then divide by 5, then add 32
....
int sampleC , sampleF;
sampleF = (sampleC - 32) * 5 / 9;
....
try "float" type variable replacing int if you need decimals.