There are various ways to find the maximum number (Largest number) among given two numbers in Java. We can use if-else condition, Ternary operator or Math.max() method in Java Programing Language.
In this tutorial we will learn finding the maximum number among given two number with the help of examples, detailed logic, and Java program Explanations for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Conditional Statements and Ternary Operator
- Math.max() method
Problem Statement
We need to implement a Java program to find maximum among two numbers. The program should take two numbers as a input and identify which is largest number between them and output the maximum number.
Examples:
Input: a=10 b=20 output: Maximum Number is : 20
Here for the given input integer 10 and 20, 20 is the maximum number among two. So the output is printed as 20.
Logic to find Maximum among Two
There are multiple logics to find the maximum number among two in Java Programing language. We can say maximum or greatest or largest among two, all are same.
Logic 1: Using if-else
- Here, we are looking for maximum number among two numbers
- First, initialize maxNum variable to Zero.
- If first number greater than second number, say maximum number is first number and update the maxNum value with first number.
- Else second number is the maximum number and update the maxNum var with second number.
- Print the maxNum value.
int maxNum=0;
if(firstNumber>secondNumber) {
maxNum=firstNumber;
System.out.println("maximum number is : " + maxNum);
}
else {
maxNum=secondNumber;
System.out.println("maximum number is : " + maxNum);
}
Logic 2: Ternary Operator
- Ternary operator takes three operands and works similar as the if else statements. If the condition true, it prints first expression and if condition becomes false then it prints second expression.
- If first number greater than second number, say maximum number is first number else second number is the maximum number.
maxNum=(firstNumber>secondNumber)?firstNumber:secondNumber;
System.out.println("Maximum number: "+ maxNum);
Java Program to Print maximum among two numbers
Program 1: Using If-Else statement
In the below program we are using if else statement to find maximum among two numbers in java
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=firstNumber;
System.out.println("maximum number is : " + maxNum);
}
else {
maxNum=secondNumber;
System.out.println("maximum number is : " + maxNum);
}
s.close();
}
}
Output
enter the first number
5
enter the second number
3
maximum number is: 5
Program Explanation to Print Max Number Using If-Else
- This Java program ask user to input two integers:
firstNumber
andsecondNumber
. - It compares these two numbers to find the maximum among them.
- If
firstNumber
is greater thansecondNumber
, it assignsfirstNumber
tomaxNum
and prints it as the maximum number. - Otherwise, it assigns
secondNumber
tomaxNum
and prints it as the maximum number. - Finally, it closes the
Scanner
object to release system resources.
Program 2: Using Ternary Operator
In the below program we are using Ternary Operator statement to find maximum among two numbers in java.
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;
maxNum=(firstNumber>secondNumber)?firstNumber:secondNumber;
System.out.println("Maximum number: "+ maxNum);
s.close();
}
}
Output
enter the first number
10
enter the second number
25
Maximum number:25
Program Explanation to print Max Number using Ternary Operator
- This Java program prompts the user to input two integers:
firstNumber
andsecondNumber
. - It uses the ternary operator
? :
to comparefirstNumber
andsecondNumber
to find the maximum among them. - If
firstNumber
is greater thansecondNumber
, it assignsfirstNumber
tomaxNum
; otherwise, it assignssecondNumber
. - It then prints the maximum number.
- Finally, it closes the
Scanner
object to release system resources.
3. Using Math.max() method
Java has inbuilt Math.max() method that takes two input integers and returns which one is greater.
public class MaxNumber {
public static void main(String[] args) {
int a =123;
double b=116.78;
System.out.println("Maximum Number is: "+ Math.max(a, b));
}
}
Output
Maximum Number is :123.0
Program Explanation to Print Max Number Using max() Method
- This Java program demonstrates the usage of the
Math.max()
method to find the maximum value between two numbers. - It defines two variables:
a
as an integer with a value of 123, andb
as a double with a value of 116.78. - It uses the
Math.max()
method to determine the maximum value betweena
andb
. - The result is then printed using
System.out.println()
, displaying “Maximum Number is: ” followed by the maximum value.
Conclusion
In this post, we explored how to find the maximum among two numbers in Java using `if-else` statement, ternary operators and the `Math.max()` method. We have seen the basic logic and then moved on to writing the Java code.
For more information on related topics, you might find these articles helpful: –
- Understanding Conditional Statements in Java
- Java Math Class: An Overview
- Top 10 Java Programming Tips
We hope this tutorial has been helpful. If you have any questions or need further clarification, feel free to leave a comment below. Don’t forget to subscribe to our newsletter for more Java programming tips and tutorials!
Happy coding!