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 largest of 3 numbers is the most common java interview question.

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

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 the largest in three numbers 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.

Also read – java program to find largest of three numbers using command line argument

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


Also read – largest of three numbers in java using if else

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


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’.

Also read – continue statement in java

In the next step user is asked to enter each and every element.

These elements or numbers are stored in an integer array ‘arrInput’.

Also read – how to create directory in java

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.

import java.util.Scanner;
public class GreatestOfNNumbers 
{
   public static void main(String[] args) 
   {
      int arrNumbers, greatest;
      Scanner sc = new Scanner(System.in);
      System.out.println("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


java program to find largest and smallest of three numbers

Let’s learn java program to find largest and smallest of three numbers.

import java.util.Scanner;
public class FindLargestAndSmallest
{
   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();
      int largest = largest(number1, number2, number3);
      int smallest = smallest(number1, number2, number3);
      System.out.println("Largest of three numbers is: " + largest);
      System.out.println("Smallest of three numbers is: " + smallest);
      sc.close();
   }
   public static int largest(int num1, int num2, int num3)
   {
      int large = num1;
      if(num2 > large)
      {
         large = num2;
      }
      if(num3 > large)
      {
         large = num3;
      }
      return large;
   }
   public static int smallest(int num1, int num2, int num3)
   {
      int small = num1;
      if(num2 < small)
      {
         small = num2;
      }
      if(num3 < small)
      {
         small = num3;
      }
      return small;
   }
}


Output:

Please enter first number: 48
Please enter second number: 87
Please enter third number: 59
Largest of three numbers is: 87
Smallest of three numbers is: 48


smallest of three numbers in java using if else

Let’s learn to find smallest of three numbers in java using if else.

public class SmallestUsingIfElse
{
   public static void main(String[] args)
   {
      int number1 = 565;
      int number2 = 268;
      int number3 = 628;
      // smallest of three
      if(number1 < number2 && number1 < number3)
      {
         System.out.println("Smallest of three numbers is: " + number1);
      }
      else if(number2 < number1 && number2 < number3)
      {
         System.out.println("Smallest of three numbers is: " + number2);
      }
      else
      {
         System.out.println("Smallest of three numbers is: " + number3);
      }
   }
}


Output:

Smallest of three numbers is: 268