Java Program to Find Greatest of Two Numbers

Greatest of two numbers using java :


In this article, we will simply create a program i.e. java program to find the greatest of two numbers. We will ask the user to enter two numbers and check which is greater. For this purpose, we use if-else statement to compare both numbers.

Greatest of two numbers using java

Working :

Step 1 : Ask the user to enter two numbers.

Step 2 : Use the condition to check which number is greater by using if-else statement.

  • If the first number is greater than the second number then print that first number is greater.
  • If the second number is greater than the first number then print that second number is greater.
  • Otherwise, both numbers are equal.

Step 3 : Print the resultant as per above conditions.

Code in Java :

//Java program to find greatest of two numbers
import java.util.Scanner;
public class greatest_of_two_numbers
public static void main(String[] args)
//scanner class declaration
Scanner sc = new Scanner(;
//input first number
System.out.print("Enter the first number : ");
int first = sc.nextInt();
//input second number
System.out.print("Enter the second number : ");
int second = sc.nextInt();
if(first > second)
System.out.println(first+" is greater than "+second);
else if(second > first)
System.out.println(second+" is greater than "+first);
System.out.println("Both numbers are Equal");
//closing scanner class(not compulsory, but good practice)

Output :

Enter the first number : 32

Enter the second number : 67

67 is greater than 32

Enter the first number : 12

Enter the second number : 12

Both numbers are Equal