ICSE Computer Science Java Programs | IT Developer
IT Developer

Java Programs - Solved 2007 ICSE Computer Science Paper



Share with a Friend

Solved 2007 ICSE Computer Science Paper

Class 10 - ICSE Computer Science Solved Papers

Menu Driven - Temperature Program - ICSE 2007 Computer Science

Using a switch statement, write a menu-driven program to convert a given temperature from Fahrenheit to Celsius and vice-versa. For an incorrect choice, an appropriate error message should be displayed.

import java.io.*; class Conversion{ public static void main(String args[])throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("1. Fahrenheit to Celsius"); System.out.println("2. Celsius to Fahrenheit"); System.out.print("Enter your choice: "); int choice = Integer.parseInt(br.readLine()); switch(choice){ case 1: System.out.print("Temperature in Fahrenheit: "); double f = Double.parseDouble(br.readLine()); double c = 5.0 / 9 * (f - 32); System.out.println("Temperature in Celsius: " + c); break; case 2: System.out.print("Temperature in Celsius: "); c = Double.parseDouble(br.readLine()); f = 1.8 * c + 32; System.out.println("Temperature in Fahrenheit: " + f); break; default: System.out.println("Invalid choice!"); } } }

Output

 
 OUTPUT 1: 
1. Fahrenheit to Celsius
2. Celsius to Fahrenheit
Enter your choice: 1
Temperature in Fahrenheit: 89.6
Temperature in Celsius: 32.0

 OUTPUT 2: 
1. Fahrenheit to Celsius
2. Celsius to Fahrenheit
Enter your choice: 2
Temperature in Celsius: 32
Temperature in Fahrenheit: 89.6