- Home
- Chapter 1 - Object Oriented Programming Concepts
- Object Oriented Programming Concepts
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 2 - Introduction to Java
- Introduction to Java
- Multiple Choice Questions
- Assignment Questions
- Chapter 3 - Values and Data Types
- Values and Data Types
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 4 - Operators in Java
- Operators in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 5 - User-Defined Methods
- User-Defined Methods
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 6 - Input in Java
- Input in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 7 - Mathematical Library Methods
- Mathematical Library Methods
- Multiple Choice Questions
- Assignment Questions
- Chapter 8 - Conditional Constructs in Java
- Conditional Constructs in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 9 - Iterative Constructs in Java
- Iterative Constructs in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions and Programs
- Chapter 10 - Nested for loops
- Nested for loops
- Assignment Questions and Programs
- Chapter 11 - Constructors
- Constructors
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 12 - Library Classes
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 13 - Encapsulation and Inheritance
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 14 - Arrays
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 15 - String Handling
- Library Classes
- Multiple Choice Questions
- Assignment Questions
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
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
