In Java Programing language, We can find the maximum (largest) number among given three numbers using various ways like using If-Else statement and Ternary Operator.
In this tutorial we will learn writing java program to find max among three along with examples, logic, algorithms and program explanations for better understanding.
Essential Java Concepts to Know Before Coding
If Else if Ladder: If the condition in the if statement is true, the program prints the statement inside the if block. When the condition is false, it will check the else if condition. Otherwise, if this else if condition is true, the program will print the statement inside the else if block. If it is still false, the program will continue to check further else if conditions. If none of the conditions are satisfied, it will print the statement inside the else block.
if(conditon){
// statements
}
else if(condition{
// statements
}else{
// statements
}
Ternary Operator: The ternary operator is the only operator that takes three operands. It works exactly like an if-else statement. Therefore, we can replace if-else statements with the ternary operator.
If the condition is true, the ternary operator returns the first value. Otherwise, it returns the second value. This makes it a concise alternative to traditional if-else statements.
variable = (condition)? Expression1: Expression2;
If the condition is true, it executes expression 1 and when condition is false it executes expression 2.
Required Knowledge
- Java programming basics
- Java Operators
- Conditional Statements and Ternary Operator
Problem Statement
We need to write a Java program that compares three numbers to determine which one is the maximum. After checking, the program should print that number as the “Maximum Number.” The program must be able to perform this action accurately, ensuring it correctly identifies the maximum number.
Examples:
Input: 10, 20,30 Output: Maximum number is : 30
Algorithm to Print Maximum among three numbers
- Start the program
- Ask the user to provide 3 inputs
- Read them and Store the values in variables
- Initialize a variable to zero, to store maximum number later
- Check the condition
- There are two ways to check conditions
- if-else:
- If (firstNum>secondNum && firstNum>thirdNum)
- Take first num as maximum, if the condition is true, if condition false then,
- else if (secondNum>firstNum && secondNum>thirdNum)
- Take second num as maximum, if the condition is true, if this condition also fails then,
- else if (thirdNum>firstNum) && (thirdNum>secondNum)
- Take third number as maximum and print the result
- If (firstNum>secondNum && firstNum>thirdNum)
- Ternary Operator:
- maxNum = (firstNum>secondNum)? firstNum : secondNum;
- If condition true, take firstNum as max and store it in maxNum, if condition false,then store second num in the maxNum.
- Take one more variable and initialize it zero, again write condition to check with third number using maxNum.
- maximumNum =(thirdNum>maxNum) ? thirdNum : maxNUm
- Now print the Maximum Number
- maxNum = (firstNum>secondNum)? firstNum : secondNum;
- if-else:
- Close the Scanner class and end the program.
Java Program to Print Max/Largest Number Among Three
Program 1: Using If-Else statement
In the below program we are using if else statement to print the maximum number among three
import java.util.Scanner;
public class MaxNumberAmongThree {
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();
System.out.println("enter the third number");
int thirdNumber =s.nextInt();
int maxNum=0;
if(firstNumber>secondNumber && firstNumber>thirdNumber) {
maxNum=firstNumber;
System.out.println("maximum number is :"+maxNum);
}
else if(secondNumber>firstNumber && secondNumber>thirdNumber){
maxNum=secondNumber;
System.out.println("maximum number is :"+maxNum);
}
else if(thirdNumber>firstNumber && thirdNumber>secondNumber){
maxNum=thirdNumber;
System.out.println("maximum number is :"+maxNum);
}
s.close();
}
}
Output:
enter the first number
10
enter the second number
25
enter the third number
15
maximum number is : 25
Program Explanation to Print Max Number Using If-Else
- The program starts by importing the Scanner class for user input.
- The
main
method is defined to execute the program. - A
Scanner
object is created to read user inputs. - The program asks the user to enter three numbers.
- Each input number is read and stored in the variables
firstNumber
,secondNumber
, andthirdNumber
. - An integer variable
maxNum
is initialized to 0. - The program uses if-else statements to compare the three numbers:
- If
firstNumber
is greater than bothsecondNumber
andthirdNumber
,maxNum
is set tofirstNumber
, and it prints the maximum number. - Otherwise, if
secondNumber
is greater than bothfirstNumber
andthirdNumber
,maxNum
is set tosecondNumber
, and it prints the maximum number. - If
thirdNumber
is greater than bothfirstNumber
andsecondNumber
,maxNum
is set tothirdNumber
, and it prints the maximum number. - The
Scanner
object is closed to free resources.
Program 2: Using Ternary Operator
In the below program we are using Ternary Operator to print maximum number among three
import java.util.Scanner;
public class MaxNumberAmongThree {
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();
System.out.println("enter the third number");
int thirdNumber =s.nextInt();
int maxNum;
maxNum=(firstNumber>secondNumber)?firstNumber:secondNumber;
int maximumNum;
maximumNum=(thirdNumber>maxNum)? thirdNumber:maxNum;
System.out.println("maximum number:" +maximumNum);
s.close();
}
}
Output:
enter the second number
1
enter the third number
23
maximum number:23
Program Explanation to print Max Number using Ternary Operator
- The program starts by importing the Scanner class for user input.
- The
main
method is defined to execute the program. - A
Scanner
object is created to read user inputs. - The program asks the user to enter three numbers.
- Each input number is read and stored in the variables
firstNumber
,secondNumber
, andthirdNumber
. - An integer variable
maxNum
is initialized to the greater value betweenfirstNumber
andsecondNumber
using the ternary operator. - Another integer variable
maximumNum
is initialized to the greater value betweenthirdNumber
andmaxNum
using the ternary operator. - The program prints the maximum number.
- The
Scanner
object is closed to free resources.
Conclusion
In this post, we explored how to find the maximum of three numbers in Java using `if-else` statement and ternary operators. 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
- Understanding ternary operator in Java
- 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!