Learn Programming and technology with ease @ developerpublish.com

HomeJavaJava Program to Find the Largest Number Among Three Numbers

# Java Program to Find the Largest Number Among Three Numbers

In this Java tutorial, we will learn how to find the largest number among three numbers using Java.Â

## Example 1: usingÂ if..elseÂ statementÂ

```publicÂ classÂ MainÂ {Â

Â Â Â Â publicÂ staticÂ voidÂ main(String[]Â args)Â {Â

Â Â Â Â Â Â Â Â doubleÂ n1Â =Â -5,Â n2Â =Â 15.55,Â n3Â =Â 50;Â

Â Â Â Â Â Â Â Â if(Â n1Â >=Â n2Â &&Â n1Â >=Â n3)Â

Â Â Â Â Â Â Â Â Â Â Â Â System.out.println(n1Â +Â "Â isÂ theÂ largestÂ number.");Â

Â Â Â Â Â Â Â Â elseÂ ifÂ (n2Â >=Â n1Â &&Â n2Â >=Â n3)Â

Â Â Â Â Â Â Â Â Â Â Â Â System.out.println(n2Â +Â "Â isÂ theÂ largestÂ number.");Â

Â Â Â Â Â Â Â Â elseÂ

Â Â Â Â Â Â Â Â Â Â Â Â System.out.println(n3Â +Â "Â isÂ theÂ largestÂ number.");Â

Â Â Â Â }Â

}```

## OUTPUTÂ

`50.0 is the largest number.`

In the above example the numbers â€“5, 15.55, 50 are stored in the variables n1, n2, n3.Â

Here using the if-else statement we find the largest number among the three numbers.Â

## Example 2: using nested if-else statementÂ

```publicÂ classÂ MainÂ {Â
Â Â Â Â publicÂ staticÂ voidÂ main(String[]Â args)Â {Â
Â Â Â Â Â Â doubleÂ aÂ =Â 0,Â bÂ =Â 5.5,Â cÂ =Â 99.9;Â
Â Â Â Â Â Â if(aÂ >=Â b)Â {Â
Â Â Â Â Â Â Â Â Â Â if(aÂ >=Â c)Â
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â System.out.println(aÂ +Â "Â isÂ theÂ largestÂ number.");Â
Â Â Â Â Â Â Â Â Â Â Â Â elseÂ
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â System.out.println(cÂ +Â "Â isÂ theÂ largestÂ number.");Â
Â Â Â Â Â Â Â Â }Â elseÂ {Â
Â Â Â Â Â Â Â Â Â Â Â Â if(bÂ >=Â c)Â
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â System.out.println(bÂ +Â "Â isÂ theÂ largestÂ number.");Â
Â Â Â Â Â Â Â Â Â Â Â Â elseÂ
Â Â  Â Â Â Â Â Â Â Â Â Â Â Â Â System.out.println(cÂ +Â "Â isÂ theÂ largestÂ number.");Â
Â Â Â Â Â Â Â Â }Â
Â Â Â Â }Â
}```

## OUTPUTÂ

`99.9 is the largest number.`

In the above example the numbers 0, 5.5, 99.9 are stored in the variables a, b, c.Â

If a is greater or equal to b and if a is greater than or equal to c, a is the greatest number else, c is the greatest number.Â

Else, if b is greater than or equal to both c and a, b is the greatest number else, c is theÂ greatestÂ number.Â

Share:

### You May Also Like

#### How to Download & Install Java on MacOS?

Java is a popular programming language that is used to develop a wide range of applications. If you are a...
• Java
• March 6, 2023

#### How to Download and Install Java for 64 bit machine?

Java is a programming language and computing platform that is used to create various types of applications. It is used...
• Java
• March 6, 2023

#### How to Download and Install JUnit in Eclipse?

In this post, youâ€™ll learn how to download and install JUnit in Eclipse so that you can use in your...
• Java
• October 9, 2022