BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to find largest of three Numbers

Last Updated: September 9, 2017 by Chaitanya Singh | Filed Under: Java Examples

Here we will write two java programs to find the largest among three numbers. 1) Using if-else..if 2) Using nested If

To understand these programs you should have the knowledge of if..else-if statement in Java. If you are new to java start from Core Java tutorial.

Example 1: Finding largest of three numbers using if-else..if

public class JavaExample{

  public static void main(String[] args) {

      int num1 = 10, num2 = 20, num3 = 7;

      if( num1 >= num2 && num1 >= num3)
          System.out.println(num1+" is the largest Number");

      else if (num2 >= num1 && num2 >= num3)
          System.out.println(num2+" is the largest Number");

      else
          System.out.println(num3+" is the largest Number");
  }
}

Output:

20 is the largest Number

Example 2: Program to find largest number among three numbers using nested if

public class JavaExample{

   public static void main(String[] args) {

      int num1 = 10, num2 = 20, num3 = 7;
        
      if(num1 >= num2) {
			
	  if(num1 >= num3)
		/* This will only execute if conditions given in both
		 * the if blocks are true, which means num1 is greater
		 * than num2 and num3
		 */
		System.out.println(num1+" is the largest Number");
	  else
	        /* This will only execute if the condition in outer if
		 * is true and condition in inner if is false. which
		 * means num1 is grater than num2 but less than num3.
		 * which means num3 is the largest
		 */
		System.out.println(num3+" is the largest Number");
      } 
      else {
			
	  if(num2 >= num3)
		/* This will execute if the condition in outer if is false
		 * and inner if is true which means num3 is greater than num1
		 * but num2 is greater than num3. That means num2 is largest
		 */
		System.out.println(num2+" is the largest Number");
	  else
		/* This will execute if the condition in outer if is false
		 * and inner if is false which means num3 is greater than num1
		 * and num2. That means num3 is largest
		 */
		System.out.println(num3+" is the largest Number");
      }
   }
}

Output:

20 is the largest Number

Top Related Articles:

  1. Java Program to Add two Binary Numbers
  2. Java Program to Calculate average using Array
  3. Java Program to Perform Arithmetic operation using Method Overloading
  4. Java Program to find largest of three numbers using Ternary Operator
  5. Java Program to Find Factorial using For and While loop

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap