Program to convert temperature from Celsius to Fahrenheit and vice-versa based on user choice.
Java
import java.util.*;
public class TemperatureCF
{
public static void main(String args[])
{
int choice=0;
double c,f=0.0;
Scanner sc=new Scanner(System.in);
System.out.println("1- To convert Fahrenheit to Celcius");
System.out.println("2- To convert Celcius to Fahrenheit");
System.out.print("Enter your choice: ");
choice=sc.nextInt();
switch(choice)
{
case 1:
{
System.out.print("Temperature in Fahrenheit: ");
f=sc.nextDouble();
c=((f-32)*5)/9;
System.out.println("Temperature in Celcius: "+c+" °C");
break;
}
case 2:
{
System.out.print("Temperature in Celcius: ");
c=sc.nextDouble();
f=((c*9)/5)+32;
System.out.println("Temperature in Fahrenheit: "+f+" °F");
break;
}
default:
{
System.out.println("Invalid Input");
}
}
}
}
Java