In this article, we are going to learn Java programs to find the greatest number among three integer numbers. There are multiple ways to solve this program and we will each of them one by one.
Approach 1: Find the Greatest using if-else statement.
In this approach, we will use if-else statements to compare the three numbers and find the greatest among them.
Java Example Code:
import java.util.Scanner; public class GreatestOfThreeNumbers { public static void main(String[] args) { int num1, num2, num3; Scanner input = new Scanner(System.in); System.out.println("Enter three numbers:"); num1 = input.nextInt(); num2 = input.nextInt(); num3 = input.nextInt(); if (num1 > num2 && num1 > num3) { System.out.println(num1 + " is the greatest."); } else if (num2 > num1 && num2 > num3) { System.out.println(num2 + " is the greatest."); } else { System.out.println(num3 + " is the greatest."); } } }
Enter three numbers:
12
23
11
23 is the greatest.
Approach 2: Using Math.max() method.
import java.util.Scanner; public class GreatestOfThreeNumbers { public static void main(String[] args) { int num1, num2, num3; Scanner input = new Scanner(System.in); System.out.println("Enter three numbers:"); num1 = input.nextInt(); num2 = input.nextInt(); num3 = input.nextInt(); int greatest = Math.max(num1, Math.max(num2, num3)); System.out.println(greatest + " is the greatest."); } }
Enter three numbers:
10
23
15
23 is the greatest.
Approach 3: Using Array and Loops.
//Java code for finding greatest number import java.util.Scanner; public class GreatestOfThreeNumbers { public static void main(String[] args) { int[] nums = new int[3]; Scanner input = new Scanner(System.in); System.out.println("Enter three numbers:"); for (int i = 0; i < 3; i++) { nums[i] = input.nextInt(); } int greatest = nums[0]; for (int i = 1; i < 3; i++) { if (nums[i] > greatest) { greatest = nums[i]; } } System.out.println(greatest + " is the greatest."); } }
Enter three numbers:
19
29
15
29 is the greatest.


