Java Program to Find Greatest of Two Numbers

Find the Greatest of the Two Numbers in Java

Given two integer inputs N1 and N2, the objective is to write a code to Find the Greatest of the Two Numbers in Java. In order to do so we’ll compare the numbers using if-else statements.
 
Example
Input : num1 = 12 and num2 = 3
Output : 12
Find the Greatest of the Two Numbers in Java

Find the Greatest of the Two Numbers in Java

Given two integer input Number1 and Number2, the objective is to write a Java code to compare both the Numbers and Find the Greatest of the Two Numbers. To do so we’ll use if-else statements and print the output. Some methods to solve the above-mentioned Problem are given below.

  • Method 1: Using if-else Statements
  • Method 2: Using Ternary Operator
  • Method 3: Using inbuilt max Function
All the above-mentioned methods are discussed in detail in the sections below.

Method 1: Using if-else Statements

In this method we’ll use if-else statements to compare the two numbers and two numbers and print out the greatest.

Working

For the two given user inputs number1 and number2

  • Check if both the integers are equal, print “Equal” if true.
  • Check if number1>number2, print number1 if true.
  • Check if number2>number1, print number2 if true.

Let’s implement the working in Java Language.

Java Code

Run
// Write a program to find the largest of two numbers in java
public class Main
 {
   public static void main (String[]args)
   {

     int num1 = 50, num2 = 20;
     if (num1 == num2)
       System.out.println ("both are equal");
     else if (num1 > num2)
         System.out.println (num1 + " is greater");

     else
         System.out.println (num2 + " is greater");

   }
 }

Output

50 is greater

Method 2: Using Ternary Operator

In this method we’ll use the Ternary Operator and compare the two numbers to check for the greatest among them.

Working

For the given integer inputs number1 and number2

  • Initialize the required variable.
  • Check for the greatest among the two using Ternary Operator.

Let’s implement the working in Java Language.

Java Code

Run
public class Main
{
  public static void main (String[]args)
  {
    int num1 = 50, num2 = 10, temp;

    if (num1 == num2)
      System.out.println ("Both are Equal\n");
    else
      {
	temp = num1 > num2 ? num1 : num2;
	System.out.println (temp + " is largest");
      }

  }
}

Output

50 is largest
						

Method 3: Using inbuilt max Function

In this method we’ll use the inbuilt max function to get the greatest of the two integer inputs.

Working

For the two integer inputs number1 and number2

  • Initialize the required variables.
  • Call the inbuilt max function with number1 and number2 as arguments.
  • Print the returned value.

Let’s implement the working in Java Language.

Java Code

Run
public class Main {
    public static void main(String args[])
    {
        int num1 = 12, num2 = 21;
        if (num1 == num2)
            System.out.println("both are equal");
        else
            // prints the maximum of two numbers
            System.out.println(Math.max(num1, num2) + " is greater");
    }
}

Output

21 is greater

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Getting Started

One comment on “Java Program to Find Greatest of Two Numbers”


  • Gyanendra

    import java.util.*;

    public class Main
    {
    public static void main(String[] args) {
    Scanner scn=new Scanner(System.in);
    int n1=scn.nextInt();
    int n2=scn.nextInt();

    int ans=n2==n1 ? 0 :n2>n1 ? n2 : n1;

    if(ans==0)
    System.out.println(“Boths are equal”);
    else
    System.out.println(ans);

    }
    }