The following program takes three integers from the user and finds the largest of those three numbers. The program uses if..elseif statement to check the greatest of three and print them accordingly.
Before we start, click on the links below to have a proper working idea of if..elseif statement that this program uses.
Program to Find the Greatest of Three Numbers in Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | //find the greatest of three numbers in java import java.util.Scanner; public class GreatestNumber { public static void main(String[] args) { int num1, num2, num3; Scanner s = new Scanner(System.in); System.out.println("Enter the first number:"); num1 = s.nextInt(); System.out.println("Enter the second number:"); num2 = s.nextInt(); System.out.println("Enter the third number:"); num3 = s.nextInt(); if(num1 > num2 && num1 > num3) { System.out.println("Largest number is:"+num1); } else if(num2 > num3) { System.out.println("Largest number is:"+num2); } else { System.out.println("Largest number is:"+num3); } } } |
Output:
Enter the first number:
34
Enter the second number:
89
Enter the third number:
23
Largest number is:89