
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Maximum of Three Numbers in Java
In this article, we will learn how to find the maximum among three numbers using an if-else statement in Java. The if-else construct allows us to evaluate conditions and execute different blocks of code based on whether the conditions are true or false. We will check the values of three numbers to determine the largest by using comparison operators and control flow statements.
Problem Statement
Given three integer values, write a Java program to find the maximum among them using if-else statements.Input
num1 = 15, num2 = -5, num3 = 7
Output
15 is the maximum number.
Steps to find the maximum among three numbers
The following are the steps to find the maximum among three numbers:
- Define three integers to compare.
- Use an if condition to check if the first number (num1) is greater than or equal to both the second (num2) and third (num3) numbers.
- If the first condition is false, use an else if condition to check if the second number (num2) is greater than or equal to both the first (num1) and third (num3) numbers.
- If neither of the above conditions is true, the third number (num3) will be the largest.
Java program to find the maximum among three numbers
The following is an example to find the maximum among the three numbers
public class Example { public static void main(String args[]) { int num1 = 15; int num2 = -5; int num3 = 7; if (num1 >= num2 && num1 >= num3) System.out.println( num1 + " is the maximum number."); else if (num2 >= num1 && num2 >= num3) System.out.println( num2 + " is the maximum number."); else System.out.println( num3 + " is the maximum number."); } }
Output
15 is the maximum number.
Code Explanation
In this program, we initialize three integer variables, num1, num2, and num3, with the values 15, -5, and 7 respectively. We then use an if statement to compare the values. We check if num1 is greater than or equal to both num2 and num3. If this condition is true, num1 is the maximum number, and it is printed as the result. If the first condition is false, we move on to the else if condition, where we check if num2 is greater than or equal to both num1 and num3. If this condition is satisfied, num2 is printed as the maximum. If neither the first nor the second condition is true, then by default, num3 must be the largest, so the program prints num3 as the maximum number.