Java program to find largest of three numbers

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

In java to find biggest number in java, comparison operator (>) is used. Comparison operator compares two numbers.

Also read – java program to find greatest of n numbers

Here’s the java program to find largest number in java,

import java.util.Scanner;

public class LargestOfThreeDemo
{ 
   public static void main(String[] args) 
   {
      int a, b, c;
      System.out.println("Please enter three integers : ");
      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("numbers are not distinct");
      }
      sc.close();
   }
}


Output:

java program to find largest of three numbers

using command line argument

Here’s the java program,

public class UsingCommandLineArgument
{
   public static void main(String[] args) 
   {
      int number1 = Integer.parseInt(args[0]);
      int number2 = Integer.parseInt(args[1]);
      int number3 = Integer.parseInt(args[2]);   
      if(number1 > number2 && number1 > number3)
      {
         System.out.println(number1 + " is largest number.");
      }
      else if(number2 > number1 && number2 > number3)
      {
         System.out.println(number2 + " is largest number.");
      }
      else   
      {
         System.out.println(number3 + " is largest number.");
      }
   }
}


Output:

javac UsingCommandLineArgument.java
java UsingCommandLineArgument 59 28 69
69 is largest number.


Largest of three numbers in java using function

In the below java program first create three integer variables ‘number1’, ‘number2’ and ‘number3’.

Initialize these three integer variables by zero. Then user enters three numbers using nextInt() method of Scanner class.

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

These three user entered numbers are stored in initially created integer variables ‘number1’, ‘number2’ and ‘number3’.

These three user entered values are passed as parameters to function ‘functionLargest()’ method.

Also read – greatest of three numbers in java using nested if

In ‘functionLargest()’ method using if else if largest number is found. Largest number found is then returned to main() method.

Lastly largest number is then printed on the console. Here’s the largest of three numbers in java using function,

import java.util.Scanner;

public class UsingFunction 
{
   public static void main(String[] args) 
   {
      int number1 = 0, number2 = 0, number3 = 0;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter first number to find largest: ");
      number1 = sc.nextInt();
      System.out.print("Please enter second number to find largest: ");
      number2 = sc.nextInt();
      System.out.print("Please enter third number to find largest: ");
      number3 = sc.nextInt();
      System.out.println("Largest of three numbers using function: " + functionLargest(number1, number2, number3));
      sc.close();
   }

   static int functionLargest(int num1, int num2, int num3)
   {
      if(num1 > num2 && num1 > num3)
      {
         return num1;
      }
      else if(num2 > num1 && num2 > num3)
      {
         return num2;
      }
      else
      {
         return num3;
      }
   }
}


Output:

Please enter first number to find largest: 59
Please enter second number to find largest: 28
Please enter third number to find largest: 29
Largest of three numbers using function: 59


Also read – java program to find largest of four numbers using nested if


Java program to find largest of four numbers using if-else

Here’s largest of four numbers using if-else,

import java.util.Scanner;

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


Output:

Please enter first number to find largest: 86
Please enter second number to find largest: 59
Please enter third number to find largest: 29
Please enter fourth number to find largest: 79
86 is largest.

This is all about largest of three numbers in java. Hope this post will be helpful for freshers in java.