Write a function that accepts three arguments a number n, and two characters s and t. The function convert n from s unit to t unit of similar physical quantities. You can choose one of the following types to use in your program:
1. Length
2. Temperature
3. Area
4. Volume
5. Weight
6. Time
#include <stdio.h>
#include <stdlib.h>
float convertTemperature(float n,char s,char t);
int main() {
float temperature;
int ch=-1;
while(ch!=7){
printf("1. Celsius to Fahrenheit\n");
printf("2. Kelvin to Fahrenheit\n");
printf("3. Fahrenheit to Celsius\n");
printf("4. Celsius to Kelvin\n");
printf("5. Kelvin to Celsius\n");
printf("6. Fahrenheit to Kelvin\n");
printf("7. Exit\n");
printf("Your choice: ");
scanf("%d",&ch);
if(ch>=1 && ch<=6){
printf("Enter the temperature: ");
scanf("%f",&temperature);
}
if(ch==1){
temperature=convertTemperature(temperature,'C','F');
printf("%.2f F\n\n",temperature);
}else if(ch==2){
temperature=convertTemperature(temperature,'K','F');
printf("%.2f F\n\n",temperature);
}else if(ch==3){
temperature=convertTemperature(temperature,'F','C');
printf("%.2f C\n\n",temperature);
}else if(ch==4){
temperature=convertTemperature(temperature,'C','K');
printf("%.2f K\n\n",temperature);
}else if(ch==5){
temperature=convertTemperature(temperature,'K','C');
printf("%.2f C\n\n",temperature);
}else if(ch==6){
temperature=convertTemperature(temperature,'F','K');
printf("%.2f K\n\n",temperature);
}else if(ch==7){
//exit
}else{
}
}
getchar();
getchar();
return 0;
}
//a function that accepts three arguments a number n, and two characters s and t.
//The function convert n from s unit to t unit of similar physical quantities.
float convertTemperature(float n,char s,char t){
//Celsius to Fahrenheit ° F = 9/5 ( ° C) + 32
if(s=='C' && t=='F'){
return ((9.0/5.0) * n) + 32;
}
//Kelvin to Fahrenheit ° F = 9/5 (K - 273) + 32
if(s=='K' && t=='F'){
return (9.0/5.0*(n - 273.15)) + 32;
}
//Fahrenheit to Celsius ° C = 5/9 (° F - 32)
if(s=='F' && t=='C'){
return (9.0/5.0)*(n- 32);
}
//Celsius to Kelvin K = ° C + 273.15
if(s=='C' && t=='K'){
return n + 273.15;
}
//Kelvin to Celsius ° C = K - 273.15
if(s=='K' && t=='C'){
return n - 273.15;
}
//Fahrenheit to Kelvin K = 5/9 (° F - 32) + 273
if(s=='F' && t=='K'){
return ((5.0/9.0) *(n - 32)) + 273.15;
}
return -1;
}
Comments
Leave a comment