In Java, to print maximum among two numbers we can use conditional statements such as if else or switch statements. Here we are using switch statement to print maximum among two.
For example, if the input numbers are 4 and 5, then 5 is the maximum number. In this article we will learn how to print maximum among two numbers using switch() statement through an example, detailed logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Switch Statements
Problem Statement
We have to write a Java program to print maximum among two. The program should take two input integer numbers and should output the maximum number in those two numbers. Let’s see an example,
Example
For given input integer numbers 9 and 3, Output should be 9.
Explanation:
Input first number = 9
Input second number =3
In number series, 9 is largest number than 3.
Therefore, maximum number is 9 among 9 and 3.
Logic to print maximum among two numbers using switch()
- Take two numbers, firstNumber and secondNumber, from the user.
- Initialize maxNum to 0.
- Use an if-else-if statement to check if the firstNumber is greater than secondNumber. If it is, set maxNum to 0.
- Otherwise, if secondNumber is greater than firstNumber, set maxNum to 1. If both conditions are false, which means both numbers are equal, set maxNum to -1.
- Use a switch statement that takes maxNum as the input value.
- Inside the switch statement, if any of the cases match the maxNum value, print the corresponding message. Use a break statement after each case to terminate the switch statement after printing the output.
Java program to print maximum among two using switch()
import java.util.Scanner;
public class MaxNumber {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the first number:");
int firstNumber = s.nextInt();
System.out.println("Enter the second number:");
int secondNumber = s.nextInt();
int maxNum = 0;
if (firstNumber > secondNumber) {
maxNum = 0;
} else if (firstNumber < secondNumber) {
maxNum = 1;
} else {
maxNum = -1; // Indicating equality
}
switch (maxNum) {
case 0:
System.out.println(firstNumber + " is the maximum number");
break;
case 1:
System.out.println(secondNumber + " is the maximum number");
break;
case -1:
System.out.println("Both numbers are equal");
break;
}
s.close();
}
}
Output:
Enter the first number
8
Enter the second number
23
23 is the maximum number
Java program explanation to print maximum among two using switch()
- The program imports the Scanner class to take user input.
- Define a class named MaxNumber with the main method.
- Inside the main method:
- Create a Scanner object
s
to read input. - Ask the user to enter the first number and store it in
firstNumber
. - Ask the user to enter the second number and store it in
secondNumber
. - Initialize an integer variable
maxNum
to 0. - Compare
firstNumber
andsecondNumber
:- If
firstNumber
is greater thansecondNumber
, setmaxNum
to 0. - Else, if
firstNumber
is less thansecondNumber
, setmaxNum
to 1. - If both numbers are equal, set
maxNum
to -1.
- If
- Use a switch statement to determine the output based on the value of
maxNum
:- If
maxNum
is 0, print thatfirstNumber
is the maximum number. - Otherwise, if
maxNum
is 1, print thatsecondNumber
is the maximum number. - If
maxNum
is -1, print that both numbers are equal.
- If
- Close the Scanner object
s
to free up resources. - This program compares two integers and outputs which one is larger or if they are equal.
Conclusion
In this article, we have learnt how to find maximum among two numbers using switch statement in Java. We have seen the logic and implemented the program. Hope you find this article helpful.
You can also check our another awesome tutorials:
- Java program to find maximum among two using if-else
- Check a given number is positive or negative using switch statement
- Java program to print ASCII character with values
- Java program to print all even numbers in a range
Happy Coding!!