Hi, I\'m taking a programming class where I am leanring C. I need help on an ass
ID: 3880115 • Letter: H
Question
Hi,
I'm taking a programming class where I am leanring C. I need help on an assignment which is called Temperature Conversion in C. I have attached the assignment below. I am trying to work on the if statements but I'm not sure how to write them correctly. Thanks!
Here is what I have so far:
#include
int main (void){
float farhenheit,celsius,kelvin;
float num;
char cha;
printf("Please enter a temperature:");
/* reading in the number plus the one letter so it cant be %f
*/
scanf("%f %c",&num,&cha);
if(cha=='c'){
return float farhenheit = 9/5(kelvin-273.15)+32;
return float kelvin = celsius + 273.15;
}
if(cha=='f'){
return float celsius = (farhenheit-32)/1.8;
return float kelvin=celsius + 273.15;
}
if(cha=='k'){
return float celsius = (farhenheit-32)/1.8;
/*if the input is celsius then convert it to fahrenheit and kelvin
*if its kelvin, convert to celsius and fahrenheit
*if its fahrenheit, convert to celsius and kelvin
*if input is not the proper value print invalid temp scale and return non-0
Explanation / Answer
#include <stdio.h> // headerfile
int main() // main function
{
float f,c,k; // declaring float values
int ch;
printf(" 1: Convert temperature from Fahrenheit to Celsius."); // user interface
printf(" 2: Convert temperature from Celsius to Fahrenheit.");
printf(" 3: Convert temperature from kelvin to Celsius.");
printf(" Enter your choice (1,2,3): ");
scanf("%d",&ch);
if(ch ==1) // right way to use 'if-else' ladder
printf(" Enter temperature in Fahrenheit. ");
scanf("%f",&f);
c= (f - 32) / 1.8;
printf("Temperature in Celsius: %.2f",c);
}
else if(ch==2){
printf(" Enter temperature in Celsius. ");
scanf("%f",&c);
f= (c*1.8)+32;
printf("Temperature in Fahrenheit: %.2f",f);
}
else if(ch==3){
printf(" Enter temperature in Kelvin. ");
scanf("%f",&c);
k= c+273.15;
printf("Temperature in Celsius: %.2f",k);
} else
{
printf(" Invalid Choice");
}
return 0; //return truee or 0
}
thank you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.