Using a switch case statement, write a menu driven program to convert a given Temperature from Fahrenheit to Celsius and vice versa. For an incorrect choice an appropriate message should be displayed. c=5/9*(f-32) and f=1.8*c+32
Answers
Question:-
- Using a switch case statement, write a menu driven program to convert a given Temperature from Fahrenheit to Celsius and vice versa. For an incorrect choice an appropriate message should be displayed.
Program:-
import java.util.*;
class Temperature
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("1. Celsius to Fahrenheit.");
System.out.println("2. Fahrenheit to Celsius.");
System.out.print("Enter your choice: ");
int n=sc.nextInt();
switch (n)
{
case 1:
System.out.print("Enter the temperature in Celsius scale: ");
double c=sc.nextDouble();
double f=1.8*c+32;
System.out.print("Temperature in Fahrenheit is: "+f);
break;
case 2:
System.out.print("Enter the temperature in Fahrenheit scale: ");
double f=sc.nextDouble();
double c=5*(f-32)/9.0;
System.out.print("Temperature in Celsius is: "+f);
break;
default:
System.out.println("Invalid Choice. Please try again later.
}
}
}
Answer:
/* a menu driven program to convert a given Temperature from Fahrenheit to Celsius and vice versa: C++ program */
#include <iostream>
using namespace std;
double fahrenheitToCelsius(double fahren)
{
double cel;
cel = (fahren - 32.0) * 5.0 / 9.0;
return cel;
}
double CelsiusTofahrenheit (double celsius )
{
float fahrenheit;
fahrenheit = (celsius * 9.0) / 5.0 + 32;
return fahrenheit;
}
int main()
{
double cel,feh;
int a;
cout<<"1. For Celsius To Fahrenheit. \n";
cout<<"2. For Fahrenheit To Celsius. \n";
cout<<"3. For Exit\n\n";
cout<<"Enter Your Choice \n ";
cin>>a;
switch(a)
{
case 1: cout<<"Enter The Temperature In Celsius\n";
cin>>cel;
feh= CelsiusTofahrenheit (cel);
cout<<"\nTemperature In Fahrenheit Is = "<<feh ;
break;
case 2: cout<<"Enter The Temperature In Fahrenheit\n";
cin>>feh;
cel= fahrenheitToCelsius(feh);
cout<<"\nTemperature In Celsius Is = "<<cel ;
break;
case 3:exit(0);
default:cout<<"\nEnter The Right Choice \n";
break;
}
}