KEMBAR78
5 Switch Case Example | PDF | Computer Engineering | Computer Science
0% found this document useful (0 votes)
5 views4 pages

5 Switch Case Example

The document provides five examples of switch-case programs in Java, including a simple calculator, a day of the week printer, a vowel or consonant checker, a month name printer from a number, and a nested switch example. Each program demonstrates the use of the switch-case structure to handle different conditions and outputs corresponding results. These examples serve as practical illustrations of how to implement switch-case statements in Java programming.

Uploaded by

Tesu Rathia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

5 Switch Case Example

The document provides five examples of switch-case programs in Java, including a simple calculator, a day of the week printer, a vowel or consonant checker, a month name printer from a number, and a nested switch example. Each program demonstrates the use of the switch-case structure to handle different conditions and outputs corresponding results. These examples serve as practical illustrations of how to implement switch-case statements in Java programming.

Uploaded by

Tesu Rathia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Here are five simple examples of switch-case programs in Java.

1. Simple Calculator
This program performs a basic arithmetic operation based on a user's choice.
public class SimpleCalculator {​
public static void main(String[] args) {​
char operator = '+';​
double num1 = 10.5;​
double num2 = 5.5;​
double result;​

switch (operator) {​
case '+':​
result = num1 + num2;​
System.out.println("Result: " + result);​
break;​
case '-':​
result = num1 - num2;​
System.out.println("Result: " + result);​
break;​
case '*':​
result = num1 * num2;​
System.out.println("Result: " + result);​
break;​
case '/':​
result = num1 / num2;​
System.out.println("Result: " + result);​
break;​
default:​
System.out.println("Invalid operator!");​
break;​
}​
}​
}​

2. Day of the Week


This example takes an integer representing a day of the week (1-7) and prints the
corresponding day name.
public class DayOfWeek {​
public static void main(String[] args) {​
int day = 4;​

switch (day) {​
case 1:​
System.out.println("Monday");​
break;​
case 2:​
System.out.println("Tuesday");​
break;​
case 3:​
System.out.println("Wednesday");​
break;​
case 4:​
System.out.println("Thursday");​
break;​
case 5:​
System.out.println("Friday");​
break;​
case 6:​
System.out.println("Saturday");​
break;​
case 7:​
System.out.println("Sunday");​
break;​
default:​
System.out.println("Invalid day!");​
break;​
}​
}​
}​

3. Vowel or Consonant
This program checks if a given character is a vowel or a consonant.
public class VowelOrConsonant {​
public static void main(String[] args) {​
char ch = 'e';​

switch (ch) {​
case 'a':​
case 'e':​
case 'i':​
case 'o':​
case 'u':​
System.out.println(ch + " is a vowel.");​
break;​
default:​
System.out.println(ch + " is a consonant.");​
break;​
}​
}​
}​
4. Month Name from Number
This example prints the name of a month corresponding to a number (1-12).
public class MonthFromNumber {​
public static void main(String[] args) {​
int month = 7;​
String monthString;​

switch (month) {​
case 1: monthString = "January";​
break;​
case 2: monthString = "February";​
break;​
case 3: monthString = "March";​
break;​
case 4: monthString = "April";​
break;​
case 5: monthString = "May";​
break;​
case 6: monthString = "June";​
break;​
case 7: monthString = "July";​
break;​
case 8: monthString = "August";​
break;​
case 9: monthString = "September";​
break;​
case 10: monthString = "October";​
break;​
case 11: monthString = "November";​
break;​
case 12: monthString = "December";​
break;​
default: monthString = "Invalid month";​
break;​
}​
System.out.println(monthString);​
}​
}​

5. Nested Switch
This program shows how a switch statement can be nested within another switch statement.
public class NestedSwitchExample {​
public static void main(String[] args) {​
char branch = 'C';​
int year = 3;​

switch(year) {​
case 1:​
System.out.println("English, Math, Science");​
break;​
case 2:​
switch(branch) {​
case 'C':​
System.out.println("Operating System, Java,
Data Structure");​
break;​
case 'E':​
System.out.println("Microprocessors, Logic
switching");​
break;​
case 'M':​
System.out.println("Thermodynamics,
Engineering Mechanics");​
break;​
}​
break;​
case 3:​
System.out.println("Computer Organization, Database,
Networking");​
break;​
default:​
System.out.println("Invalid Year or Branch!");​
}​
}​
}​

You might also like