Write a C function convertTemp that converts the temperature
Solution
void convertTemp(double temperature,char tempUnit){
double result = 0;
if(tempUnit==\'F\' || tempUnit==\'f\'){
result = temperature-32;
result *= ((double)5/9);
cout<< result << endl;
}
else if(tempUnit==\'C\' || tempUnit==\'c\'){
result = temperature*((double)9/5);
result += 32;
cout<< result << endl;
}
else{
cout << \"Please enter either \'F\' or \'C\'!!!\" << endl;
}
}
Here is the function which converts the temperature from fahrenheit to celsius and celsius to fahrenheit.
First line is the function definition , if we check that line we have,
void convertTemp(double temperature,char tempUnit){
void : This is callled return type of the function , as void means nothing , we are telling the compiler that the function returns nothing , so don’t wait for any values after calling.
convertTemp : This is the method name that we are using.
The variables that are inside the parenthesis are called arguments , as specified in the problem we have two arguments those are
1) temperature variable to store temperature value which could hold decimal values also ,
2) tempUnit variable which is used to find whether the number given by user is in Fahrenheit or Celsius using first letter which is of char datatype.
Coming to second line, it is result variable declaration. This variable is gonna hold our final result.
Now we use if-elseif-else condition to check whether the input is in fahrenheit or celsius or something else. If the character is `F` or `f` , then we will use one formula and if it is `C` or `c` then we will use different formula , and if the input is not falls under above two conditions then we will tell the user to enter proper character.
If the input character is `F` or `f` , then we will use the formula to convert from fahrenheit to celsius , as formula suggests , we have to subtract 32 first and the divide the result with 5/9 and then finally print the result on the screen.
If the input character is `C` or `c` , then we will use the formula to convert from celsius to fahrenheit, as formula suggests , we have to multiply it with 9/5 first and the add 32 to the result and then finally print the result on the screen
Note : We should be careful while doing division operations because if we don’t specify or cast any of them to double type , then it will give us integer values by rounding off the values.
.That’s it , congrates. Your function should work great.
You can use your method by calling it like below
convertTemp(54,\'F\'); // function calling

