In: Computer Science
LANGUAGE: C
Only using <stdio.h> & <stdlib.h>
Write a program that gives the user a menu to choose from –
1. Convert temperature input from the user in degrees Fahrenheit to degrees Celsius
2. Convert temperature input from the user in degrees Celsius to degrees Fahrenheit
3. Quit.
Formulae you will need: C = (5 / 9) * (F-32) and F = (9/5) * C + 32
1. Use functions to accomplish 1 and 2 above.
2. Use at least one function for each scenario (i.e. converting temperature from degrees Fahrenheit to degrees Celsius and from degrees Celsius to degrees Fahrenheit). You must call them from the main function. You can use more functions if you see fit to do so, but use at least 4 functions.
Test the program with the following values:
68 degree F = 20 degree C
20 degree C = 68 degree F
-22 degree F = -30 degree C
0 degree C = 32 degree F
Code:
#include<stdio.h> // header file which contains basic
input output functions
#include<stdlib.h> // header file for general purpose
standard library
int cal_temp_cel(); //function declaration
int cal_cel_temp(int tem1); //function declaration
int cal_temp_far(); //function declaration
int cal_far_temp(int tem3); //function declaration
int display_menu(); //function declaration
int main() //main function
{
int ch,tem1,tem2,tem3,tem4,flag=0; // variable declaration and
initialization
while(flag!=3) // the loop will run till the value of flag becomes
3
{
ch=display_menu(); //function call to display the menu
switch(ch) //input for menu
{
case 1:
tem1=cal_temp_cel(); //function call to enter the value
tem2=cal_cel_temp(tem1); //function call to calculate the
value
printf("%d degree F = %d degree C \n",tem1,tem2); //display the
final result
break;
case 2:
tem3=cal_temp_far(); //function call to enter the value
tem4=cal_far_temp(tem3); //function call to calculate the
value
printf("%d degree C = %d degree F \n",tem3,tem4); //display the
final result
break;
case 3:
exit(0);
default:
printf("Please Enter a valid Choice! \n"); // to handle invalid
choices
exit(0);
}
}
return 0;
}
int display_menu() // menu
{
int choice;
printf("1. Convert temperature input from the user in degrees
Fahrenheit to degrees Celsius \n");
printf("2. Convert temperature input from the user in degrees
Celsius to degrees Fahrenheit \n");
printf("3. Quit \n");
printf("Enter Your Choice: ");
scanf("%d",&choice);
return choice;
}
int cal_temp_cel() //function to take the input for
Fahrenheit
{
int far;
printf("Enter the value in Fahrenheit Scale: ");
scanf("%d",&far);
return far;
}
int cal_cel_temp(int tem1) //function to calculate the value in
celcius
{
int c;
c=(tem1-32)*5/9; //formula
return c;
}
int cal_temp_far() //function to take the input for celcius
{
int cel;
printf("Enter the value in Celsius Scale: ");
scanf("%d",&cel);
return cel;
}
int cal_far_temp(int tem3) //function to calculate the value in
Fahrenheit
{
int f;
f=(tem3*9/5)+32; //formula
return f;
}
For your reference
Code:
Output: