In: Computer Science
MUST BE DONE IN C (NOT C++)
Your create a program that can implement the cases in which the initial unit is Fahrenheit or something not recognizable. Your program should incorporate Fahrenheit to Celsius, Fahrenheit to Kelvin and unknown initial units (display an error message for this last one). You must use functions to calculate Fahrenheit degrees.
#include<stdio.h>
//function to convert from fahrenheit to kelvin
float FahrenheitToKelvin(float temperature)
{
float kelvin;
//expression to convert fahrenheit to
kelvin
kelvin =
(temperature-32)*(float)5/9+273.15;
return kelvin;//return kelvin
}
//function to convert from fahrenheit to celcius
float FahrenheitToCelcius(float temperature)
{
float celcius;
//expression to convert fahrenheit
to celcius
celcius = (temperature - 32)*(float)5/9;
return celcius;//return celcius
}
//driver program
int main()
{
//variable declarations
float celcius,kelvin,fahrenheit;
int opt;
printf("\n Enter the temperature in
Fahrenheit");
scanf("%f",&fahrenheit); //read the
temperature in fahrenheit
//show the options
printf("\n 1. Fahrenheit to Kelvin.\n 2.
Fahrenheit to Celcius.");
printf("\n Enter the choice : ");
scanf("%d",&opt);//read the option
//condition to call
FahrenheitToKelvin(fahrenheit);
if(opt==1)
{
kelvin =
FahrenheitToKelvin(fahrenheit);
printf("\n%5.1f F = %5.3f
K",fahrenheit,kelvin);
}
else
//condition to call
FahrenheitToCelcius(fahrenheit);
if(opt==2)
{
celcius =
FahrenheitToCelcius(fahrenheit);
printf("\n%5.1f F = %5.3f
K",fahrenheit,celcius);
}
else//block for
invalid choice
printf("\n ERROR
: Invalid Choice for conversion.");
return 0;
}
output