Java program to find the largest in three numbers using nested if

Let’s learn java program to find the largest in three numbers using nested if.

java program to find the largest in three numbers using nested if

Java program to find the largest in three numbers using nested if

Java program to find largest of 3 numbers using nested if is the most common java interview question.

In the below greatest of three numbers in java using nested if rather checking a condition in single if else statement we use nested if to find largest in three numbers.

Also read – preface to java virtual machine and architecture

Here’s the java program to find largest in three number in java using nested if.

public class LargestNestedIfDemo 
{
   public static void main(String[] args) 
   {
      int num1 = 36, num2 = 35, num3 = 56;
      if(num1 >= num2) 
      {
         if(num1 >= num3)
         {
            System.out.println(num1 + " is largest number.");
         }
         else
         {
            System.out.println(num3 + " is largest number.");
         }
      } 
      else 
      {
         if(num2 >= num3)
         {
            System.out.println(num2 + " is largest number.");
         }
         else
         {
            System.out.println(num3 + " is largest number.");
         }
      }
   }
}


Output:

56 is largest number.


Java program to find largest of four numbers using nested if

In the below java program user input is taken using nextInt() method of scanner class.

First user is asked to enter four numbers one by one. These four numbers is stored in four integer variables number1, number2, number3 and number4.

Then using java nested if we find largest of four numbers. Let’s learn java program to find largest of four numbers using nested if,

import java.util.Scanner;

public class LargestOfFourNumbers 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter first number: ");
      int number1 = sc.nextInt();
      System.out.println("Please enter second number: ");
      int number2 = sc.nextInt();
      System.out.println("Please enter third number: ");
      int number3 = sc.nextInt();
      System.out.println("Please enter first number: ");
      int number4 = sc.nextInt();
      if(number1 > number2)
      {
         if(number1 > number3)
         {
            if(number1 > number4)
            {  
               System.out.println("Largest of four numbers is: " + number1);
            }
            else
            {
               System.out.println("Largest of four numbers is: " + number4);
            }
         }
      }
      else if(number2 > number3)
      {
         if(number2 > number4)
         {
            System.out.println("Largest of four numbers is: " + number2);
         }
         else
         {
            System.out.println("Largest of four numbers is: " + number4);
         }
      }
      else if(number3 > number4)
      {
         System.out.println("Largest of four numbers is: " + number3);
      }
      else
      {
         System.out.println("Largest of four numbers is: " + number4);
      }
      sc.close();
   }
}


Output:

Please enter first number: 18
Please enter second number: 43
Please enter third number: 98
Please enter first number: 28
Largest of four numbers is: 98


Largest of three numbers in java using if else

In the below example we take input from user using nextInt() method of scanner class. First user enters three numbers.

These three numbers are stored in three integer variables number1, number2 and number3.

Also read – largest of three numbers in java using ternary operator

Then to find largest of three numbers we have to check below conditions using java if else.

  1. if number1 is greater than or equal to both number2 and number3 then number1 is largest.
  2. if number2 is greater than or equal to both number1 and number3 then number2 is largest.
  3. Lastly number3 is largest.

Here’s largest of three numbers in java using if else,

import java.util.Scanner;

public class LargestOfThreeIfElse 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter first number: ");
      int number1 = sc.nextInt();
      System.out.println("Please enter second number: ");
      int number2 = sc.nextInt();
      System.out.println("Please enter third number: ");
      int number3 = sc.nextInt();
      if(number1 >= number2 && number1 >= number3)
      {
         System.out.println(number1 + " is the largest number using if else.");
      }
      else if(number2 >= number1 && number2 >= number3)
      {
         System.out.println(number2 + " is the largest number using if else.");
      }
      else
      {
         System.out.println(number3 + " is the largest number using if else.");
      }
      sc.close();
   }
}


Output:

Please enter first number: 56
Please enter second number: 86
Please enter third number: 78
86 is the largest number using if else.


Java program to find greatest of n numbers

To find largest number in an array take input from user using nextInt() method scanner class.

First user is asked to input total number of elements in an array. This number is stored in integer variable ‘arrNumbers’.

In the next step user is asked to enter each and every element. These elements or numbers are stored in an integer array ‘arrInput’.

Finally compare elements of array with each other to find greatest number or largest number in an array and print on the console.

Here’s the java program to find greatest of n numbers or java program to find the largest number in an array.

import java.util.Scanner;

public class GreatestOfNNumbers 
{
   public static void main(String[] args) 
   {
      int arrNumbers, greatest;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter total number of elements in array: ");
      arrNumbers = sc.nextInt();
      int[] arrInput = new int[arrNumbers];
      System.out.println("Please enter " + arrNumbers + " elements of array: ");
      for(int a = 0; a < arrNumbers; a++)
      {
         arrInput[a] = sc.nextInt();
      }
      greatest = arrInput[0];
      for(int a = 0; a < arrNumbers; a++)
      {
         if(greatest < arrInput[a])
         {
            greatest = arrInput[a];
         }
      }
      System.out.println("Greatest of n numbers: " + greatest);
      sc.close();
   }
}


Output:

Please enter total number of elements in array: 6
Please enter 6 elements of array:
44
28
89
75
18
86
Greatest of n numbers: 89