Find the Greatest Number Among Three Numbers in Java
Find the Greatest of the Three Numbers in Java
Given three integers num1, num2 and num3 as inputs. The objective is to write a code to Find the Greatest of the Three Numbers in Java Language. To do so we’ll check the numbers with each other and print the largest of them all.
Example Input : num1 = 12 num2 = 9 num3 = 14 Output : 14
Find the Greatest of the Three Numbers in Java
Given Three integer inputs num1, num2 and num3, the objective is ti write a code to Find the Largest of the Three Numbers in Java Language. In this article we will see a Java program to Find Greatest of three numbers. We will use if else conditions and ternary operator too to find the same. Here are some of the methods to solve the above mentioned problem,
- Method 1: Using if-else Statements 2
- Method 2: Using if-else Statements 2
- Method 3: Using Ternary Operator
We’ll discuss the above mentioned methods in the sections below in depth.
Method 1: Using if-else Statements 1
Java Code
Run
public class Main { public static void main (String[]args) { int num1 = 10, num2 = 20, num3 = 30; //checking if num1 is greatest if (num1 >= num2 && num1 >= num3) System.out.println (num1 + " is the greatest"); //checking if num2 is greatest else if (num2 >= num1 && num2 >= num3) System.out.println (num2 + " is the greatest"); //checking if num2 is greatest else if (num3 >= num1 && num3 >= num2) System.out.println (num3 + " is the greatest"); } }
Output
30 is the greatest
Method 2: Using if-else Statements 2
Java Code
Run
public class Main { public static void main (String[]args) { int num1 = 10, num2 = 20, num3 = 30; //comparing num1 with other numbers if ((num1 >= num2) && (num1 >= num3)) System.out.println (num1 + " is the greatest"); //checking if num2 is greatest else if (num2 >= num1 && num2 >= num3) System.out.println (num2 + " is the greatest"); // num3 has to be greatest then if not above else System.out.println (num3 + " is the greatest"); } }
Output
30 is the greatest
Method 3: Using Ternary Operator
In this method we use the knowledge of Ternary Operators in Java.
Ternary Operator Syntax
( Condition ) ? ( if True : Action ) : ( if False : Action )
Java Code
Run
public class Main { public static void main (String[]args) { int num1 = 10, num2 = 20, num3 = 30; int temp, result; // find the largest b/w num1 and num2 & store in temp temp = num1>num2 ? num1:num2; // find the largest b/w temp and num3 & finally printing it result = temp>num3 ? temp:num3; System.out.println (result + " is the greatest"); } }
Output
30 is the greatest
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
- 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
- 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
Login/Signup to comment