Java program to find largest of three numbers

Today you will learn java program to find largest of three numbers.

Also read – find largest of three using ternary operator

In java to find largest of three numbers, comparison operator (>) is used. Comparison operator compares two numbers.

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