In: Physics
Implement the following function:
a. float temp(float t, char ch) – this function takes temperature
‘t’ and unit of
temperature ‘ch’ as parameter. The variable ‘ch’ will be either f
or c depending on
whether the temperature in variable ‘t’ is in Fahrenheit or
Celsius. The function
returns the converted temperature to the main method.
Call the above function in the main method. Initialize temperature
and unit of
temperature from the user and pass them as parameter to the above
function. The
converted temperature should be displayed in the main method.
Hi teacher can you please give me solution in c++ & python
The C++ code is as under
########################
#include <iostream>
using namespace std;
float temp(float t, char ch) {
if (ch == 'C' || ch == 'c') {
return t*1.8;
}
else if (ch == 'F'|| ch == 'f') {
return t/1.8;
}
else {
float t;
char ch;
cout<<"Please input temperature, only numbers";
cin>>t;
cout<<"Please input C for celcius and F for
Fahrenheit";
cin>>ch;
cout<<temp(t,ch);
}
}
int main()
{
float t;
char ch;
cout<<"Please input temperature, only numbers";
cin>>t;
cout<<"Please input C for celcius and F for
Fahrenheit";
cin>>ch;
if(ch == 'F'|| 'f'){
cout<<temp(t,ch);
cout<<"Converted to Celcius";
}
else if(ch == 'C'|| 'c'){
cout<<temp(t,ch);
cout<<"Converted to F";
}
return 0;
}
##########################
the python code is as under
##################
def temp(t,ch):
if(ch == 'C' || ch == 'c'):
return t*1.8
else if(ch == 'F' || ch == 'f'):
return t/1.8
else:
return temp(input('Please input temperature, only numbers'),
input('Please input C for celcius and F for Fahrenheit')
if __name__=="__main__":
t = input('Please input temperature, only numbers');
x = input('Please input C for celcius and F for Fahrenheit');
if(x=="C"||x=="c"):
print(temp(t,x ))
print("Converted to F")
else:
print(temp(t,x ))
print("Converted to C")