#define c_to_k(x) x+273.15
#define k_to_c(x) x-273.15
#define c_to_f(x) (1.8)*x+32
#define f_to_c(x) (x-32)*(5.0/9)
#define f_to_k(x) c_to_k(f_to_c(x))
#define k_to_f(x) c_to_f(k_to_c(x))
void convert_temp()
{
int unit1=0,unit2=0;
long double val;
enum {zero,k,f,c};
line;
printf("\n[1] Kelvin(K)\n[2] Fahrenheit(°F) \n[3] Celsius(°C)\n");
line;
printf("\nConvert from : ");
scanf("%d",&unit1);
printf("\nConvert to : ");
scanf("%d",&unit2);
line;
if(unit1==k)
{
printf("\nEnter the temperature in K : ");
scanf("%Lf",&val);
if(unit2==k)
printf("\nTemperature in K : %Lf",val);
else if(unit2==f)
printf("\nTemperature in °F : %Lf",k_to_f(val));
else if(unit2==c)
printf("\nTemperature in °C : %Lf",k_to_c(val));
}
else if(unit1==f)
{
printf("\nEnter the temperature in °F : ");
scanf("%Lf",&val);
if(unit2==k)
printf("\nTemperature in K : %Lf",f_to_k(val));
else if(unit2==f)
printf("\nTemperature in °F : %Lf",val);
else if(unit2==c)
printf("\nTemperature in °C : %Lf",f_to_c(val));
}
else if(unit1==c)
{
printf("\nEnter the temperature in °C : ");
scanf("%Lf",&val);
if(unit2==k)
printf("\nTemperature in K : %Lf",c_to_k(val));
else if(unit2==f)
printf("\nTemperature in °F : %Lf",c_to_f(val));
else if(unit2==c)
printf("\nTemperature in °C : %Lf",val);
}
}
I tested using the converter in my phone. The rest give same results. Only from Kelvin to Fahrenheit, I don't get the values correct