Let’s learn java program to find largest of three numbers.
Java program to find largest of three numbers
In the below java program first take three numbers as input from the user using nextInt() method of Scanner class.
These three numbers are stored in three integer variables a, b and c. Now using below logic we find the greatest of three numbers using if-else…if
- if(a >= b && a >= c) – compare numbers, a with b and a with c. if both conditions are true, print a.
- else if(b >= a && b >= c) – compare b with a and b with c. if both conditions are true, print b.
- finally print c if the above conditions are false.
Here’s the program to find largest of three numbers.
import java.util.Scanner; public class LargestOfThreeNumbersInJava { public static void main(String[] args) { int a, b, c; System.out.println("Please enter three numbers: "); Scanner sc = new Scanner(System.in); a = sc.nextInt(); b = sc.nextInt(); c = sc.nextInt(); if(a >= b && a >= c) System.out.println(a + " is the largest number."); else if(b >= a && b >= c) System.out.println(b + " is the largest number."); else System.out.println(c + " is the largest number."); sc.close(); } }
Output:

Please enter three numbers:
56
86
76
86 is the largest number.
Let’s see another example to find largest of three numbers
In the below program we are going to find greatest of three numbers without getting input from user or without using Scanner class.
public class LargestOfThreeNumbersInJava { public static void main(String[] args) { int number1 = 52, number2 = 76, number3 = 83; if(number1 >= number2 && number1 >= number3) System.out.println(number1 + " is the largest number."); else if(number2 >= number1 && number2 >= number3) System.out.println(number2 + " is the largest number."); else System.out.println(number3 + " is the largest number."); } }
Output:
83 is the largest number.
Also read – major features of java