Write a program to find maximum between two numbers using switch case in Java

The switch statement allows us to execute a block of code among many alternatives.

The syntax of the switch statement in Java is:

switch (expression) { case value1: // code break; case value2: // code break; ... ... default: // default statements }

How does the switch-case statement work?

The expression is evaluated once and compared with the values of each case.

  • If expression matches with value1, the code of case value1 are executed. Similarly, the code of case value2 is executed if expression matches with value2.
  • If there is no match, the code of the default case is executed.

Note: The working of the switch-case statement is similar to the Java if...else...if ladder. However, the syntax of the switch statement is cleaner and much easier to read and write.

Example: Java switch Statement

// Java Program to check the size // using the switch...case statement class Main { public static void main(String[] args) { int number = 44; String size; // switch statement to check size switch (number) { case 29: size = "Small"; break; case 42: size = "Medium"; break; // match the value of week case 44: size = "Large"; break; case 48: size = "Extra Large"; break; default: size = "Unknown"; break; } System.out.println("Size: " + size); } }

Output:

Size: Large

In the above example, we have used the switch statement to find the size. Here, we have a variable number. The variable is compared with the value of each case statement.

Since the value matches with 44, the code of case 44 is executed.

size = "Large"; break;

Here, the size variable is assigned with the value Large.

Recommended Reading: Create a Simple Calculator Using the Java switch Statement

Flowchart of switch Statement

Write a program to find maximum between two numbers using switch case in Java
Flow chart of the Java switch statement

break statement in Java switch...case

Notice that we have been using break in each case block.

... case 29: size = "Small"; break; ...

The break statement is used to terminate the switch-case statement. If break is not used, all the cases after the matching case are also executed. For example,

class Main { public static void main(String[] args) { int expression = 2; // switch statement to check size switch (expression) { case 1: System.out.println("Case 1"); // matching case case 2: System.out.println("Case 2"); case 3: System.out.println("Case 3"); default: System.out.println("Default case"); } } }

Output

Case 2 Case 3 Default case

In the above example, expression matches with case 2. Here, we haven't used the break statement after each case.

Hence, all the cases after case 2 are also executed.

This is why the break statement is needed to terminate the switch-case statement after the matching case. To learn more, visit Java break Statement.

default case in Java switch-case

The switch statement also includes an optional default case. It is executed when the expression doesn't match any of the cases. For example,

class Main { public static void main(String[] args) { int expression = 9; switch(expression) { case 2: System.out.println("Small Size"); break; case 3: System.out.println("Large Size"); break; // default case default: System.out.println("Unknown Size"); } } }

Output

Unknown Size

In the above example, we have created a switch-case statement. Here, the value of expression doesn't match with any of the cases.

Hence, the code inside the default case is executed.

default: System.out.println("Unknown Size);

class Maxoftwo { public static void main(String args[]) { //taking value as command line argument. //Converting String format to Integer value int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]); if(i> j) System.out.println(i+" is greater than "+j); else System.out.println(j+" is greater than "+i); } }

 OUTPUT:

C:\java>javac Maxoftwo.java C:\java>java Maxoftwo 10 20 20 is greater than 10 C:\java>java Maxoftwo 30 10 30 is greater than 10

Note: C:\>java> is a directory where source and class files exist.


  • Write a C program to find maximum of the two numbers using switch case statement.
  • How to find maximum of the two numbers using switch statement.


  • C printf and scanf functions
  • Switch case statement in C

We will first take two numbers as input from user using scanf function. Then we print the maximum number on screen using switch case statement.

#include <stdio.h> #include <conio.h> int main() { int a, b; /* Take two numbers as input from user using scanf function */ printf("Enter Two Integers\n"); scanf("%d %d", &a, &b); switch(a > b) { /* a>b comparison returns true(1) */ case 1: printf("%d is Maximum", a); break; /* a>b comparison returns false(0) */ case 0: printf("%d is maximum", b); break; } getch(); return 0; } Output Enter Two Integers 4 8 8 is Maximum Enter Two Integers -2 -4 -2 is Maximum
Related Topics


Page 2

  • Write a C program to find maximum of the two numbers using conditional or ternary operator.
  • How to find largest of the two numbers using conditional statement.


  • C printf and scanf functions
  • Conditional Operator in C

In this program, we will use conditional operator to find maximum of two numbers.

#include <stdio.h> int main() { int a, b, maximum; /* Take two numbers as input from user using scanf function */ printf("Enter Two Integers\n"); scanf("%d %d", &a, &b); if(a == b){ printf("Both Equal\n"); return 0; } /* Finds maximum using Ternary Operator */ maximum = (a > b) ? a : b; printf("%d is Maximum\n", maximum); return 0; } Output Enter Two Integers 10 14 14 is Maximum
Related Topics

Write a C program to input two numbers from user and find maximum between two numbers using switch case. How to find maximum or minimum between two numbers using switch case. Logic to find maximum between two numbers using switch case in C programming.

Example
InputInput first number: 12 Input second number: 40

Output

Maximum: 40

In my previous posts, I explained various ways to find maximum or minimum using other approaches.

Must learn -

In this post I will explain to find maximum using switch...case. Finding maximum using switch...case is little tricky and under-the-hood concept.

So, let us begin with prerequisites first.

Required knowledge

Basic C programming, Relational operator, Switch case statement

Logic to find maximum using switch...case statement

In all our previous exercises on switch...case we switched variable value. However, you can also write an expression inside switch.

The expression num1 > num2 evaluates 1 if num1 is greater than num2 otherwise evaluates 0. So if we write switch(num1 > num2), there can be two possible cases case 0 and case 1.

Step by step descriptive logic to find maximum using switch...case.

  1. Input two numbers from user. Store it in some variable say num1 and num2.
  2. Switch expression switch(num1 > num2).
  3. For the expression (num1 > num2), there can be two possible values 0 and 1.
  4. Write case 0 and print num2 is maximum.
  5. Write case 1 and print num1 is maximum.

Important note: There is no possibility of default case in this program.

Program to find maximum using switch...case statement

/** * C program to find maximum between two numbers using switch case */ #include <stdio.h> int main() { int num1, num2; /* Input two numbers from user */ printf("Enter two numbers to find maximum: "); scanf("%d%d", &num1, &num2); /* Expression (num1 > num2) will return either 0 or 1 */ switch(num1 > num2) { /* If condition (num1>num2) is false */ case 0: printf("%d is maximum", num2); break; /* If condition (num1>num2) is true */ case 1: printf("%d is maximum", num1); break; } return 0; }

Enter two numbers to find maximum: 20 10 20 is maximum

Happy coding 😉