Java Program to Find Largest of Three Numbers

In this section, we will learn how to create a Java program to find the largest of three numbers. Along with this, we will also learn to find the largest of three numbers in Java using the ternary operator.

Using Ternary Operator

Before moving to the program, let’s understand the ternary operator.

It is a part of the Java conditional statement. It contains three operands. It evaluates a Boolean expression and assigned a value based on the result. We can use it in place of the if-else statement. The syntax of the ternary operator is as follows:

  1. variable_name = (expression) ? value if true:value if false  

If the above expression returns true the value before the colon is assigned to the variable variable_name, else the value after the colon is assigned to the variable.

Let’s use the ternary operator in a Java program to compare three variables.

In the following program, we have used two ternary operators to compare three numbers.

LargestNumberExample1.java

  1. import java.util.Scanner;  
  2. public class LargestNumberExample1  
  3. {  
  4. public static void main(String[] args)   
  5. {  
  6. int a, b, c, largest, temp;  
  7. //object of the Scanner class  
  8. Scanner sc = new Scanner(System.in);  
  9. //reading input from the user  
  10. System.out.println(“Enter the first number:”);  
  11. a = sc.nextInt();  
  12. System.out.println(“Enter the second number:”);  
  13. b = sc.nextInt();  
  14. System.out.println(“Enter the third number:”);  
  15. c = sc.nextInt();  
  16. //comparing a and b and storing the largest number in a temp variable  
  17. temp=a>b?a:b;  
  18. //comparing the temp variable with c and storing the result in the variable  
  19. largest=c>temp?c:temp;  
  20. //prints the largest number  
  21. System.out.println(“The largest number is: “+largest);  
  22. }  
  23. }  

Output:

Enter the first number:
23
Enter the second number:
11
Enter the third number:
67
Largest Number is: 67

We can also compare all the three numbers by using the ternary operator in a single statement. If we want to compare three numbers in a single statement, we must use the following statement.

  1. d = c > (a>b ? a:b) ? c:((a>b) ? a:b);  

In the following program, we have used a single statement to find the largest of three numbers.

LargestNumberExample2.java

  1. import java.util.Scanner;  
  2. public class LargestNumberExample2  
  3. {  
  4. public static void main(String[] args)   
  5. {  
  6. int a, b, c, largest;  
  7. //object of the Scanner class  
  8. Scanner sc = new Scanner(System.in);  
  9. //reading input from the user  
  10. System.out.println(“Enter the first number:”);  
  11. a = sc.nextInt();  
  12. System.out.println(“Enter the second number:”);  
  13. b = sc.nextInt();  
  14. System.out.println(“Enter the third number:”);  
  15. c = sc.nextInt();  
  16. largest = c > (a > b ? a : b) ? c : ((a > b) ? a : b);  
  17. System.out.println(“The largest number is: “+largest);  
  18. }  
  19. }  

Output:

Enter the first number:
45
Enter the second number:
87
Enter the third number:
34
The largest number is: 87

Using if-else..if

LargestNumberExample3.java

  1. public class LargestNumberExample3  
  2. {  
  3. public static void main(String[] args)   
  4. {  
  5. //initializing numbers to compare  
  6. int a=40, b=78, c=19;  
  7. //comparing numbers, a with b and a with c   
  8. //if both conditions are true, prints a  
  9. if(a>=b && a>=c)  
  10. System.out.println(a+” is the largest Number”);  
  11. //comparing b with a and b with c  
  12. //if both conditions are true, prints b  
  13. else if (b>=a && b>=c)  
  14. System.out.println(b+” is the largest Number”);  
  15. else  
  16. //prints c if the above conditions are false  
  17. System.out.println(c+” is the largest number”);  
  18. }  
  19. }  

Output:

78 is the largest number

Using nested if

LargestNumberExample4.java

  1. public class LargestNumberExample4  
  2. {  
  3. public static void main(String[] args)   
  4. {  
  5. //initializing numbers to compare  
  6. int x = 1010, y = 170, z = 169;  
  7. if(x >= y)   
  8. {  
  9. if(x >= z)  
  10. //prints x, if the above two conditions are true  
  11. System.out.println(“The largest number is: “+x);  
  12. else  
  13. //prints z, if the condition defined in inner if is true and the condition defined in inner if is false  
  14. //means x>y and x<z  
  15. System.out.println(“The largest number is: “+z);  
  16. }   
  17. else   
  18. {  
  19. if(y >= z)  
  20. //prints y, if the condition defined in outer if is false and the condition defined in inner if is true  
  21. //means z>x and y>z  
  22. System.out.println(“The largest number is: “+y);  
  23. else  
  24. //prints z, if the condition defined in both inner and outer loop is false  
  25. //z>x and z>y  
  26. System.out.println(“The largest number is: “+z);  
  27. }  
  28. }  
  29. }  

Output:

The largest number is: 1010

LargestNumberExample5.java

  1. import java.util.Scanner;  
  2. public class LargestNumberExample5  
  3. {  
  4. public static void main(String args[])  
  5. {  
  6. int num1, num2, num3;  
  7. System.out.println(“Enter three integers: “);  
  8. Scanner in = new Scanner(System.in);  
  9. num1=in.nextInt();  
  10. num2=in.nextInt();  
  11. num3=in.nextInt();  
  12. if (num1 > num2 && num1 > num3)  
  13. System.out.println(“The largest number is: “+num1);  
  14. else if (num2 > num1 && num2 > num3)  
  15. System.out.println(“The largest number is: “+num2);  
  16. else if (num3 > num1 && num3 > num2)  
  17. System.out.println(“The largest number is: “+num3);  
  18. else  
  19. System.out.println(“The numbers are same.”);  
  20. }  
  21. }  

Output 1:

Enter three integers: 
12 110 9
The largest number is: 110

Output 2:

Enter three integers: 
9 9 9
The numbers are same.

Leave a Comment