Java Program to Find Largest of Three Numbers

In this section, we will explore how to write a Java program that determines the largest of three numbers. Additionally, we will also examine the approach of using the ternary operator to find the largest of three numbers in Java.

Using Ternary Operator

Before we proceed with the program, let’s take a moment to familiarize ourselves with the ternary operator in Java.

The ternary operator is a component of Java’s conditional statement. It consists of three operands and is utilized to evaluate a Boolean expression, assigning a value based on the result. It can be employed as an alternative to an if-else statement. The syntax of the ternary operator is as follows:

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

If the condition specified in the ternary expression is true, the value preceding the colon is assigned to the variable. Otherwise, the value following the colon is assigned to the variable.

We will now create a Java program that utilizes the ternary operator to compare three variables.

In the provided program, two ternary operators are used to compare three numbers.

LargestNumberExample.java

import java.util.Scanner;  
public class LargestNumberExample  
{  
public static void main(String[] args)   
{  
int a, b, c, largest, temp;  
//object of the Scanner class  
Scanner sc = new Scanner(System.in);  
//reading input from the user  
System.out.println("Enter the first number:");  
a = sc.nextInt();  
System.out.println("Enter the second number:");  
b = sc.nextInt();  
System.out.println("Enter the third number:");  
c = sc.nextInt();  
//comparing a and b and storing the largest number in a temp variable  
temp=a>b?a:b;  
//comparing the temp variable with c and storing the result in the variable  
largest=c>temp?c:temp;  
//prints the largest number  
System.out.println("The largest number is: "+largest);  
}  
}

Output:

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

To compare all three numbers in a single statement using the ternary operator, we can utilize the following expression.

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

LargestNumberExample1.java

import java.util.Scanner;  
public class LargestNumberExample1  
{  
public static void main(String[] args)   
{  
int a, b, c, largest;  
//object of the Scanner class  
Scanner sc = new Scanner(System.in);  
//reading input from the user  
System.out.println("Enter the first number:");  
a = sc.nextInt();  
System.out.println("Enter the second number:");  
b = sc.nextInt();  
System.out.println("Enter the third number:");  
c = sc.nextInt();  
largest = c > (a > b ? a : b) ? c : ((a > b) ? a : b);  
System.out.println("The largest number is: "+largest);  
}  
}

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

LargestNumberExample2.java

public class LargestNumberExample2  
{  
public static void main(String[] args)   
{  
//initializing numbers to compare  
int a=40, b=78, c=19;  
//comparing numbers, a with b and a with c   
//if both conditions are true, prints a  
if(a>=b && a>=c)  
System.out.println(a+" is the largest Number");  
//comparing b with a and b with c  
//if both conditions are true, prints b  
else if (b>=a && b>=c)  
System.out.println(b+" is the largest Number");  
else  
//prints c if the above conditions are false  
System.out.println(c+" is the largest number");  
}  
}

Output:

78 is the largest number

Using nested if

LargestNumberExample3.java

public class LargestNumberExample3  
{  
public static void main(String[] args)   
{  
//initializing numbers to compare  
int x = 1010, y = 170, z = 169;  
if(x >= y)   
{  
if(x >= z)  
//prints x, if the above two conditions are true  
System.out.println("The largest number is: "+x);  
else  
//prints z, if the condition defined in inner if is true and the condition defined in inner if is false  
//means x>y and x<z  
System.out.println("The largest number is: "+z);  
}   
else   
{  
if(y >= z)  
//prints y, if the condition defined in outer if is false and the condition defined in inner if is true  
//means z>x and y>z  
System.out.println("The largest number is: "+y);  
else  
//prints z, if the condition defined in both inner and outer loop is false  
//z>x and z>y  
System.out.println("The largest number is: "+z);  
}  
}  
}

Output:

The largest number is: 1010

LargestNumberExample4.java

import java.util.Scanner;  
public class LargestNumberExample4  
{  
public static void main(String args[])  
{  
int num1, num2, num3;  
System.out.println("Enter three integers: ");  
Scanner in = new Scanner(System.in);  
num1=in.nextInt();  
num2=in.nextInt();  
num3=in.nextInt();  
if (num1 > num2 && num1 > num3)  
System.out.println("The largest number is: "+num1);  
else if (num2 > num1 && num2 > num3)  
System.out.println("The largest number is: "+num2);  
else if (num3 > num1 && num3 > num2)  
System.out.println("The largest number is: "+num3);  
else  
System.out.println("The numbers are same.");  
}  
}

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.

For additional learning resources, such as the Java program to find the GCD of two numbers, consider following tutorials.freshersnow.com.