You are here

Java program to find largest of three numbers

Java program to find the largest of three numbers and then print it. If the numbers are unequal then "numbers are not distinct" is printed. Comparison operator '>' is used to compare two numbers. To find the largest number out of a given numbers you can also use an array.

Java programming source code

  1. import java.util.Scanner;
  2.  
  3. class Largest
  4. {
  5.   public static void main(String args[])
  6.   {
  7.     int x, y, z;
  8.     System.out.println("Enter three integers");
  9.     Scanner in = new Scanner(System.in);
  10.  
  11.     x = in.nextInt();
  12.     y = in.nextInt();
  13.     z = in.nextInt();
  14.  
  15.     if (x > y && x > z)
  16.       System.out.println("First number is largest.");
  17.     else if (y > x && y > z)
  18.       System.out.println("Second number is largest.");
  19.     else if (z > x && z > y)
  20.       System.out.println("Third number is largest.");
  21.     else
  22.       System.out.println("The numbers are not distinct.");
  23.   }
  24. }

Output of program:
largest numbers

Download Largest of three numbers program class file.

If you want to find out largest number from a list of say 10 integers then using above approach isn't easy, instead you can use an array.