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
- 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 largest.");
- else if (y > x && y > z)
- System.out.println("Second number is largest.");
- else if (z > x && z > y)
- System.out.println("Third number is largest.");
- System.out.println("The numbers are not distinct.");
Output of program:
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.