# Java Program To Find Greatest Among Three Numbers

Problem :- Write A C++ Program To Find Greatest Among Three Numbers .Take three numbers from user and print the greatest number among them ex :- three numbers are 10 ,20 ,30 then 30 is greatest show 30 in output screen .

Logic :- For this problem there is a simple logic that take all three numbers and choose one number and compare that number to rest of the two number is number is greatest then print else choose second number and repeat the previous process if greatest then print second number is greatest else go to third number and repeat the same process .We are taking a Example and check all the process Step by Step

Example :- We are taking a Three number 55,65,75 as Input

Step 1 :- Select first number 55 and compare to 65 and 75 as we know 55 is not greater so control transfer to second condition .

Step 2 :- Now choose Second number 65 and compare to 55 and 75 as we know 65 is greater that 55 but not greater than 75 so control transfer to Third condition .

Step 3 :- Now choose Third number 75 and compare to 65 and 55 as we know 75 is greater compare both number 55 and 65 .Now we have a greatest number then print the number

Step 4 :- This is a Special Step if all condition didn't Match then All Number are equal .Example :- 50 ,50 ,50  then control goes to last condition and program  Output is "All Are Equal "

Solution :-

Method 1:-

Visit http://www.programmingwithbasics.com/
*/

import java.util.Scanner;

class GreatestAmongThree
{
public static void main(String[] args)
{
int num1,num2,num3;
Scanner scan=new Scanner(System.in);
System.out.println("Enter Three Numbers: ");
num1=scan.nextInt();
num2=scan.nextInt();
num3=scan.nextInt();

if(num1>=num2 && num1>=num3)
{
System.out.println("Greatest Number Is : "+num1);
}
else if(num2>=num1 && num2>=num3)
{
System.out.println("Greatest Number Is : "+num2);
}
if(num3>=num1 && num3>=num2)
{
System.out.println("Greatest Number Is : "+num3);
}
}

}

Method 2:-Find Greatest Using Ternary Operator

Visit http://www.programmingwithbasics.com/
*/

import java.util.Scanner;
class GreatestAmongThree
{
public static void main(String[] args)
{
int num1,num2,num3,Greatest;

Scanner scan=new Scanner(System.in);
System.out.println("Enter Three Numbers: ");

num1=scan.nextInt();
num2=scan.nextInt();
num3=scan.nextInt();

Greatest=(num1>num2)?(num1>num3?num1:num3):(num2>num3?num2:num3);
System.out.println("Greatest Number Is : "+Greatest);
}

}

Output:-