Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

## Largest Among Three Numbers in Java

In this article we will see different ways to find the largest among three numbers.

### Method-1 : By using ternary operator

By suing ternary operator we can find the largest number among three numbers.

Synatx of ternary operator-: variable_name = (expression) ? value if true:value if false

**Approach:**

- Take three numbers input from user.
- Store it in 3 int variables
`first`

,`second`

and`third`

. - Using ternary operator check the largest number among
`first`

and`second`

. - Store the largest among these two in an temporary integer variable
`temp`

. - Now compare
`temp`

value with`third`

value using ternary operator. - Store the largest among these two in an integer variable
`big`

. - big value represent the largest number.

**Program:**

import java.util.*; public class Largest { public static void main(String[] args) { int first, second, third, big, temp; //Scanner class object created to take input Scanner sc = new Scanner(System.in); //Taking input from the user System.out.println("Enter the first number:"); first = sc.nextInt(); System.out.println("Enter the second number:"); second = sc.nextInt(); System.out.println("Enter the third number:"); third = sc.nextInt(); // compare first number with second number using ternary operator // Store largest number among them in a temporary variable temp temp=first>second?first:second; //Then compare the temp variable with third variable using ternary operator big=third>temp?third:temp; //prints the largest number System.out.println("The largest among three numbers: "+big); } }

Output : Enter the first number: 4 Enter the second number: 2 Enter the third number: 8 The largest among three numbers: 8

### Method-2 : By using if-else statement

By suing if-else statement we can find the largest number among three numbers.

**Approach:**

- Take three numbers input from user.
- Store it in 3 int variables
`first`

,`second`

and`third`

. - In the first if condition compare first number with other two numbers if it is larger than other two numbers then first number is largest.
- In the second else if condition compare second number with other two numbers if it is larger than other two numbers then second number is largest.
- If no one is bigger then, Otherwise third number is largest.

Program:

import java.util.*; public class Largest { public static void main(String[] args) { int first, second, third, big, temp; //Scanner class object created to take input Scanner sc = new Scanner(System.in); //Taking input from the user System.out.println("Enter the first number:"); first = sc.nextInt(); System.out.println("Enter the second number:"); second = sc.nextInt(); System.out.println("Enter the third number:"); third = sc.nextInt(); //comparing first with second and third value if(first>=second && first>=third) System.out.println("The largest among largest numbers: "+first); //comparing second with first and third value else if (second>=first && second>=third) System.out.println("The largest among largest numbers: "+second); // else third value is largest else System.out.println("The largest among largest numbers: "+third); } }

Output : Enter the first number: 7 Enter the second number: 9 Enter the third number: 2 The largest among largest numbers: 9

### Method-2 : By using nested if statement

By suing nested if statement we can find the largest number among three numbers.

**Approach:**

- Take three numbers input from user.
- Store it in 3 int variables
`first`

,`second`

and`third`

. - In the first if condition comparing first number with second number
- If first number is greater so inner if condition now will be excuted
- Then comparing first number with third number
- If first is greater than third number then first is largest number
- otherwise third is largest number
- If the first if condition fails means second is largest
- So else part will be executed.
- Now if second number is greater than third number then second is largest.
- Other wise third number is largest.

**Program:**

import java.util.*; public class Largest { public static void main(String[] args) { int first, second, third, big, temp; //Scanner class object created to take input Scanner sc = new Scanner(System.in); //Taking input from the user System.out.println("Enter the first number:"); first = sc.nextInt(); System.out.println("Enter the second number:"); second = sc.nextInt(); System.out.println("Enter the third number:"); third = sc.nextInt(); //comparing first number with second number if(first >= second) { // first number is greater so this condition now will be excuted // comparing first number with third number // if first is greater than third number then first is largest number // otherwise third is largest number if(first >= third) System.out.println("The largest among largest numbers: "+first); else System.out.println("The largest among largest numbers: "+third); } // means first if condition failed so this else will be executed // means second is largest so first if condition failed // now if second number is greater than third number then second is largest // other wise third number is largest else { if(second >= third) System.out.println("The largest among largest numbers: "+second); else System.out.println("The largest among largest numbers: "+third); } } }

Output : Enter the first number: 9 Enter the second number: 2 Enter the third number: 8 The largest among largest numbers: 9

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

**Related Java Basic Programs:**

- Java Program to Print an Integer (Entered by the User)
- Java Program to Add Two Integers
- Java Program to Multiply two Floating Point Numbers
- Java Program to Find ASCII Value of a character
- Java Program to Compute Quotient and Remainder
- Java Program to Swap Two Numbers
- Java Program to Check Whether a Number is Even or Odd
- Java Program to Find the Frequency of Character in a String
- Java Program to Remove All Whitespaces from a String
- Java Program to Round a Number to n Decimal Places
- Java Program to Check if a String is Empty or Null