Java Program to Find the Largest Among Three Numbers


In this article, we will understand how to find the largest of three integers in Java. This is done using a greater than operator (<). Here we use a simple if-else condition to compare the values.

Below is a demonstration of the same −

Input

Suppose our input is −

-50, 30 and 50

Output

The desired output would be −

The largest number is 50

Algorithm

Step1- Start
Step 2- Declare three integers: input_1, input_2 and input_3
Step 3- Prompt the user to enter the three-integer value/ define the integers
Step 4- Read the values
Step 5- Using an if else loop, compare the first input with the other two inputs to check if it is
the largest of the three integers. If not, repeat the step for the other two integers.
Step 6- Display the result
Step 7- Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool run button.

import java.util.Scanner;
public class Largest {
   public static void main(String[] args) {
      int my_input_1, my_input_2, my_input_3;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A scanner object has been defined ");
      System.out.print("Enter the first number : ");
      my_input_2 = my_scanner.nextInt();
      System.out.print("Enter the second number : ");
      my_input_2 = my_scanner.nextInt();
      System.out.print("Enter the third number : ");
      my_input_3 = my_scanner.nextInt();
      my_input_1 = -50;
      my_input_2 = 30;
      my_input_3 = 50;
      if( my_input_1 >= my_input_2 && my_input_1 >= my_input_3)
         System.out.println("The largest number is " +my_input_1);
      else if (my_input_2 >= my_input_1 && my_input_2 >= my_input_3)
         System.out.println("The largest number is " +my_input_2);
      else
         System.out.println("The largest number is " +my_input_3);
   }
}

Output

Required packages have been imported
A reader object has been defined
Enter the first number : -50
Enter the second number : 30
Enter the third number : 50
The largest number is 50

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class Largest {
   public static void main(String[] args) {
      int my_input_1, my_input_2, my_input_3;
      my_input_1 = -50;
      my_input_2 = 30;
      my_input_3 = 50;
      System.out.println("The three numbers are defined as " +my_input_1 +", " +my_input_2 +" and " +my_input_3);
      if( my_input_1 >= my_input_2 && my_input_1 >= my_input_3)
         System.out.println("The largest number is " +my_input_1);
      else if (my_input_2 >= my_input_1 && my_input_2 >= my_input_3)
         System.out.println("The largest number is " +my_input_2);
      else
         System.out.println("The largest number is " +my_input_3);
   }
}

Output

The three numbers are defined as -50, 30 and 50
The largest number is 50

Updated on: 21-Feb-2022

586 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements