Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

Conditional Constructs in Java

Chapter 8

Conditional Constructs in Java

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Java Program: Temperature Conversion Using Switch Case


34. Using switch case, write a program to convert temperature from Fahrenheit to Celsius and Celsius to Fahrenheit.
Fahrenheit to Celsius formula: (32°F - 32) x 5/9
Celsius to Fahrenheit formula: (0°C x 9/5) + 32

Java Programs

import java.util.Scanner;

 

public class TemperatureConversion {

    public static void main(String[] args) {

 

        // Title

        System.out.println("TEMPERATURE CONVERSION USING SWITCH CASE");

        System.out.println("----------------------------------------");

 

        Scanner sc = new Scanner(System.in);

 

        System.out.println("Menu");

        System.out.println("1. Fahrenheit to Celsius");

        System.out.println("2. Celsius to Fahrenheit");

        System.out.print("Enter your choice: ");

        int choice = sc.nextInt();

 

        double temp, result;

 

        switch (choice) {

            case 1:

                System.out.print("Enter temperature in Fahrenheit: ");

                temp = sc.nextDouble();

                result = (temp - 32) * 5 / 9;

                System.out.println("Temperature in Celsius = " + result);

                break;

 

            case 2:

                System.out.print("Enter temperature in Celsius: ");

                temp = sc.nextDouble();

                result = (temp * 9 / 5) + 32;

                System.out.println("Temperature in Fahrenheit = " + result);

                break;

 

            default:

                System.out.println("Invalid choice!");

        }

 

        sc.close();

    }

}

Output

Sample Run 1 

Menu
1. Fahrenheit to Celsius
2. Celsius to Fahrenheit
Enter your choice: 1
Enter temperature in Fahrenheit: 98
Temperature in Celsius = 36.666666666666664

Sample Run 2 

Menu
1. Fahrenheit to Celsius
2. Celsius to Fahrenheit
Enter your choice: 2
Enter temperature in Celsius: 37
Temperature in Fahrenheit = 98.6

📝 Explanation

  • Program uses switch-case for menu selection
  • Takes temperature input using Scanner
  • Applies the correct formula based on the choice
  • Displays converted temperature