Java Program to Find Greatest of Two Numbers
Find the Greatest of the Two Numbers in Java
Example Input : num1 = 12 and num2 = 3 Output : 12
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
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
// 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
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
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
- ASCII Table
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Finding Prime Factors of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
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);
}
}