Java Program To Find Largest Between Three Numbers

Sharing is healthy !!

Hey guys!!! Welcome to flower brackets blog. Today you will learn java program to find largest between three numbers.

Here in the example scanner class is used to take input from the user. Let’s see maximum number in java,

EXAMPLE: Java Program To Find Largest Between Three Numbers

Java Program To Find Largest Between Three Numbers

Also Read – Java Program To Find Even Or Odd

Java program to find largest of three numbers using if else,

import java.util.Scanner;

public class LargestOfThree
{
   public static void main(String[] args)
   {
      int a;
      int b;
      int c;

      System.out.println("Enter three numbers to find the largest: ");
      Scanner sc = new Scanner(System.in);

      a = sc.nextInt();
      b = sc.nextInt();
      c = sc.nextInt();

      if(a > b && a > c)
      {
         System.out.println("First number is largest.");
      }
      else if(b > a && b > c)
      {
         System.out.println("Second number is largest.");
      }
      else if(c > a && c > b)
      {
         System.out.println("Third number is largest.");
      }
      else
      {
         System.out.println("Either numbers are not distinct.");
      }
   }
}

Output:

Enter three numbers to find the largest: 34 76 86
Third number is largest.


using ternary operator

Now let’s learn java program to find largest of 3 numbers using ternary operator,

import java.util.Scanner;

public class LargestOfThree
{
   public static void main(String[] args)
   {
      int num1, num2, num3, num4;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter three numbers to find the largest : ");
      num1 = sc.nextInt();
      num2 = sc.nextInt();
      num3 = sc.nextInt();
      num4 = num3 > (num1 > num2 ? num1 : num2) ? num3 : ((num1 > num2) ? num1 : num2);
      System.out.println("The largest number : " + num4);
      sc.close();
   }
}

Output:

Please enter three numbers to find the largest :
5
10
15
The largest number : 15


using nested if

Here we are going to learn java program to find largest of three numbers using nested if.

public class LargestOfThreeDemo
{
   public static void main(String[] args)
   {
      int num1 = 5, num2 = 10, num3 = 15;
 
      if(num1 >= num2)
      { 
         if(num1 >= num3)
            System.out.println(num1 + " is the largest Number");
         else
            System.out.println(num3 + " is the largest Number");
      } 
      else
      { 
         if(num2 >= num3)
            System.out.println(num2 + " is the largest Number");
         else
            System.out.println(num3 + " is the largest Number");
      }
   }
}

Output:

15 is the largest Number


using function

Now let’s learn how to find largest number in java using function.

import java.util.Scanner;

public class LargestOfThreeExample
{
   static int Largest(int num1, int num2, int num3)
   {
      if( num1 > num2 && num1 > num3)
         return num1;
      else if(num2 > num1 && num2 > num3)
         return num2;
      else
         return num3;
   }
 
   public static void main(String[] args)
   {
      int num1 = 0, num2 = 0, num3 = 0;
      Scanner sc = new Scanner(System.in); 
      System.out.print("Please enter first no. : ");
      num1 = sc.nextInt(); 
      System.out.print("Please enter second no. : ");
      num2 = sc.nextInt(); 
      System.out.print("Please enter third no. : ");
      num3 = sc.nextInt(); 
      System.out.println("The largest number is : " + Largest(num1, num2, num3));
      sc.close();
   }
}

Output:

Please enter first number : 10
Please enter second number : 50
Please enter third number : 100
The largest number is : 100


conclusion

That’s it guys. This is all about largest number in java. I hope you have understood the program.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

Sharing is healthy !!