You are here

Java program to find the largest of three numbers

Java program to find the largest of three numbers, 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 given numbers you can also use an array.

Java programming source code

import java.util.Scanner;

class Largest
{
  public static void main(String args[])
  {
    int x, y, z;
    System.out.println("Enter three integers");
    Scanner in = new Scanner(System.in);

    x = in.nextInt();
    y = in.nextInt();
    z = in.nextInt();

    if (x > y && x > z)
      System.out.println("First number is the largest.");
    else if (y > x && y > z)
      System.out.println("Second number is the largest.");
    else if (z > x && z > y)
      System.out.println("Third number is the largest.");
    else
      System.out.println("The numbers are not distinct.");
  }
}

Output of program:
largest numbers

Download Largest of three numbers program class file.

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

Share this