The program shows how to convert Fahrenheit to Celsius and Vice-Versa in java using a switch statement. The program uses a scanner class to take the inputs from the user.
Switch case is mostly used when it is necessary to give options to the users. The Java program below gives you two options to choose from case 1 and case 2 that calculate the conversion of Fahrenheit to Celsius and Celsius to Fahrenheit respectively.
If you do not know the working process of the switch case statement then click the link below.
Java Program to Convert Fahrenheit to Celsius and Vice-Versa using Switch Case
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | //convert Fahrenheit to Celsius and vice-versa using switch in java import java.util.Scanner; class FahrenheitCelsius { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Press 1 to convert Celsius into Fahrenheit"); System.out.println("Press 2 to convert Fahrenheit into Celsius"); System.out.println("Enter your Choice:"); int ch = sc.nextInt(); switch(ch) { case 1: System.out.println("Enter temperature in Fahrenheit:"); float F = sc.nextFloat(); float C = ((5/9.0f)*(F-32)); System.out.println("Temperature in Centigrade degrees:"+C); break; case 2: System.out.println("Enter temperature in Celsius:"); float c = sc.nextFloat(); float f = ((c - 32)*5)/9; System.out.println("Temperature in Fahrenheit degrees:"+f); break; default:System.out.println("Invalid Input"); break; } } } |
Output:
1 2 3 4 5 6 7 | Press 1 to convert Celsius into Fahrenheit Press 2 to convert Fahrenheit into Celsius Enter your Choice: 2 Enter temperature in Celsius: 41 Temperature in Fahrenheit degrees:5.0 |