In: Computer Science
Problem:
Design and write a C language program that can be used as a unit converter application. Your unit converter should contain at least four unit categories, for example: length, mass, temperature, and time. The program should display the main menu that contains unit categories that are available, and the user will be prompted to select a unit category first. After the unit category has been selected the program should then display another menu (i.e., a submenu) that contains at least three units or more for the chosen unit category. For example, if the mass unit category is selected, the units shown in its submenu could be: kilogram, pound, and ounce. The user will be prompted next to enter a value (i.e., a floating point number) to be converted, as well as the two units from the submenu. The first unit is the unit to be converted, while the second unit is the unit to which the first unit will be converted. After these two units are selected, the program will compute and display the result of the conversion. The program will then prompt the user to return to the main menu and repeat the same process, or terminate the program. Program requirements: It is required to use the following C language constructs: · User-Defined Functions . switch/case · Loops · Decisions
Please upvote ,comment for any query i will update , Thanks .
Note : check attached image for output and program Code compiled and tested in Code blocks C IDE.
Program :
/*
length have three unit convrsion meter ,centimeter ,milimeter
mass have three unit kilogram ,ounce ,pound
temperature have three unit fahrenheit ,celsius ,kelvin
time also have three unit conversion seconds, minutes ,hours
*/
#include<stdio.h>
#include<conio.h>
float tempConversion()
{
float value;
int first,second;
printf("Enter value to convert:"); //prompt user for float value to
convert
scanf("%f",&value);
printf("1. value is fahrenheit\n"); //prompt user to choice unit of
value entered by him
printf("2. value is celsius\n");
printf("3. value is kelvin\n");
printf("Enter choice:");
scanf("%d",&first);
switch(first)
{
case 1:
printf("1. to convert in celsius\n"); //prompt to convert in
printf("2. to convert in kelvin\n");
break;
case 2:
printf("1. to convert in fahrenheit\n");
printf("2. to convert in kelvin\n");
break;
case 3:
printf("1. to convert in fahrenheit\n");
printf("2. to convert in celsius\n");
break;
default:
break;
}
printf("Enter choice:"); //choice of unit to convert in
scanf("%d",&second);
if(first==1 && second==1) //value is fahrenheit and
convert in celsius
{
float celsius= (0.55555555555555555555555555555556 *(value -
32));
printf("fahrenheit value %.2f in celsius
is:%.2f\n",value,celsius);printf("\n");
}
else if(first==1 && second==2) //value is fahrenheit and
convert in kelvin
{
float kelvin =(0.55555555555555555555555555555556 *(value- 32) +
273);
printf("fahrenheit value %.2f in kelvin
is:%.2f\n",value,kelvin);printf("\n");
}
else if(first==2 && second==1) //value is celsius and
convert in fahrenheit
{
float fah= 1.8*value + 32;
printf("celsius value %.2f in fahrenheit
is:%.2f\n",value,fah);printf("\n");
}
else if(first==2 && second==2) //value is celsius convert
in kelvin
{
float kelvin = value+ 273;
printf("celsius value %.2f in kelvin
is:%.2f",value,kelvin);printf("\n");
}
else if(first==3 && second==1) //kevin to fah
{
float fah= (1.8* (value - 273) + 32);
printf("kelvin value %.2f in fahrenheit
is:%.2f",value,fah);printf("\n");
}
else if(first==3 && second==2) //kelvin to celsius
{
float celsius= value - 273;
printf("kelvin value %.2f in celsius
is:%.2f",value,celsius);printf("\n");
}
}
float lengthConversion()
{
float value;
int first,second;
printf("Enter value to convert:");
scanf("%f",&value);
printf("1. value is meter\n");
printf("2. value is centimeter\n");
printf("3. value is milimeter\n");
printf("Enter choice:");
scanf("%d",&first);
switch(first)
{
case 1:
printf("1. to convert in centimeter\n");
printf("2. to convert in milimeter\n");
break;
case 2:
printf("1. to convert in meter\n");
printf("2. to convert in milimeter\n");
break;
case 3:
printf("1. to convert in meter\n");
printf("2. to convert in centimeter\n");
break;
default:
break;
}
printf("Enter choice:");
scanf("%d",&second);
if(first==1 && second==1)
{
printf("meter value %.2f in centimeter
is:%.2f",value,value*100);printf("\n");
}
else if(first==1 && second==2)
{
printf("meter value %.2f in milimeter
is:%.2f",value,value*1000);printf("\n");
}
else if(first==2 && second==1)
{
printf("meter value %.2f in milimeter
is:%.2f",value,value/100.0);printf("\n");
}
else if(first==2 && second==2)
{
printf("meter value %.2f in milimeter
is:%.2f",value,value*10);printf("\n");
}
else if(first==3 && second==1)
{
printf("meter value %.2f in milimeter
is:%.2f",value,value/1000.0);printf("\n");
}
else if(first==3 && second==2)
{
printf("meter value %.2f in milimeter
is:%.2f",value,value/10.0);printf("\n");
}
}
float massConversion()
{
float value;
int first,second;
printf("Enter value to convert:");
scanf("%f",&value);
printf("1. value is kilogram\n");
printf("2. value is ounce\n");
printf("3. value is pound\n");
printf("Enter choice:");
scanf("%d",&first);
switch(first)
{
case 1:
printf("1. to convert in ounce\n");
printf("2. to convert in pound\n");
break;
case 2:
printf("1. to convert in kilogram\n");
printf("2. to convert in pound\n");
break;
case 3:
printf("1. to convert in kilogram\n");
printf("2. to convert in ounce\n");
break;
default:
break;
}
printf("Enter choice:");
scanf("%d",&second);
if(first==1 && second==1)
{
float ounce=value*35.274;
printf("kilogram value %.2f in ounce
is:%.2f",value,ounce);printf("\n");
}
else if(first==1 && second==2)
{
float pound=value*2.205;
printf("kilogram value %.2f in pound
is:%.2f",value,pound);printf("\n");
}
else if(first==2 && second==1)
{
float kg=value/35.274;
printf("ounce value %.2f in kilogram
is:%.2f",value,kg);printf("\n");
}
else if(first==2 && second==2)
{
float pd=value/16;
printf("ounce value %.2f in pound
is:%.2f",value,pd);printf("\n");
}
else if(first==3 && second==1)
{
float kg=value/2.205;
printf("pound value %.2f in kilogram
is:%.2f",value,kg);printf("\n");
}
else if(first==3 && second==2)
{
float ounce=value*16;
printf("pound value %.2f in ounce
is:%.2f",value,ounce);printf("\n");
}
}
float timeConversion()
{
float value;
int first,second;
printf("Enter value to convert:");
scanf("%f",&value);
printf("1. value is seconds\n");
printf("2. value is minutes\n");
printf("3. value is hours\n");
printf("Enter choice:");
scanf("%d",&first);
switch(first)
{
case 1:
printf("1. to convert in minutes\n");
printf("2. to convert in hours\n");
break;
case 2:
printf("1. to convert in seconds\n");
printf("2. to convert in hours\n");
break;
case 3:
printf("1. to convert in seconds\n");
printf("2. to convert in minutes\n");
break;
default:
break;
}
printf("Enter choice:");
scanf("%d",&second);
if(first==1 && second==1)
{
printf("seconds value %.2f in minutes
is:%.2f",value,value/60.0);printf("\n");
}
else if(first==1 && second==2)
{
printf("seconds value %.2f in hours
is:%.2f",value,value/3600.0);printf("\n");
}
else if(first==2 && second==1)
{
printf("minutes value %.2f in seconds
is:%.2f",value,value*60);printf("\n");
}
else if(first==2 && second==2)
{
printf("minutes value %.2f in hours
is:%.2f",value,value/60.0);printf("\n");
}
else if(first==3 && second==1)
{
printf("hours value %.2f in seconds
is:%.2f",value,value*60*60);
printf("\n"); //choice for length
}
else if(first==3 && second==2)
{
printf("hours value %.2f in minutes is:%.2f",value,value*60);
printf("\n");
}
}
int main()
{
int exitFlag=0;
while(1)
{
int choice;
printf("1. length conversion\n");
printf("2. mass conversion\n");
printf("3. temperature conversion\n");
printf("4. time conversion\n");
printf("5. Exit\n");
printf("Enter choice:");
scanf("%d",&choice);
if(choice==1) //choice for length
{
int newChoice;
lengthConversion(); //convert and display the unit
while(1)
{
printf("1. return to main menu\n"); //again prompt user for
choice
printf("2. repeat same process\n");
printf("3. terminate program\n");
scanf("%d",&newChoice);
if(newChoice==1)
{
break; //return to main menu
}
else if(newChoice==2)
{
lengthConversion(); //again convert values
}
else
{
exitFlag=1; //terminate the program
break;
}
}
}
else if(choice==2) //choice for mass
{
int newChoice;
massConversion();
while(1)
{
printf("1. return to main menu\n");
printf("2. repeat same process\n");
printf("3. terminate program\n");
scanf("%d",&newChoice);
if(newChoice==1)
{
break;
}
else if(newChoice==2)
{
massConversion();
}
else
{
exitFlag=1;
break;
}
}
}
else if(choice==3) //temperature choice
{
int newChoice;
tempConversion();
while(1)
{
printf("1. return to main menu\n");
printf("2. repeat same process\n");
printf("3. terminate program\n");
scanf("%d",&newChoice);
if(newChoice==1)
{
break;
}
else if(newChoice==2)
{
tempConversion();
}
else
{
exitFlag=1;
break;
}
}
}
else if(choice==4) //time conversion
{
int newChoice;
timeConversion();
while(1)
{
printf("1. return to main menu\n");
printf("2. repeat same process\n");
printf("3. terminate program\n");
scanf("%d",&newChoice);
if(newChoice==1)
{
break;
}
else if(newChoice==2)
{
timeConversion();
}
else
{
exitFlag=1;
break;
}
}
}
else if(choice==5)
{
printf("bye ,see you again later!\n"); //terminate program for main
menu
break;
}
else
{
printf("Invalid choice\n"); //prompt user for invalid choice
}
if(exitFlag) //when exit flag is one terminate the program from
sub menu
{
printf("good bye ,See you again later!\n");
exitFlag=0;
break;
}
}
}
Output :