12/16/2016

Java Program to Find Largest Among Three Numbers

Problem:- Java program to find largest of three numbers or Java Program to Find the Largest Among Three Numbers or Java Program to Find Largest Between Three Numbers Using Ternary operator or Java Program to Find the Biggest of 3 Numbers or Biggest of three numbers using Logical Operators in Java or How to find largest of three Integers in Java. Write a C Program for finding the greatest number among given three number.

Logic:- To solve this problem we need to compare all three numbers each other .for better explanation we need three number assume that numbers are 17, 45, 56. ex-you have to give three number, a number may be Integer, Float, Double your task is to find the Greatest number Input given by the user.



Step 1:- First We Compare 17 to 45 and 56 if it is greatest then we can say that 17 is greatest else we go to next step

Step 2:- Then we compare 45 to 56 and 17 if it is greatest then we can say that 45 is greatest else we go to next step

Step 3:- Then we compare 56 to 45 and 17 if it is greatest then we can say that 56 is greatest else we go to next step (Special Case)

Step 4:- This is the last case if all numbers are same or equal then it will print a message that

" All INPUT ARE EQUAL "


See Also:- C++ Program To Find Greater Number. Among Given Three Number

Method 1:- Java Program to Find largest Among Three Numbers



import java.util.Scanner;
/* Program By Ghanendra Yadav
   Visit http://www.programmingwithbasics.com/
*/
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 largest Using Ternary Operator



import java.util.Scanner;
/* Program By Ghanendra Yadav
   Visit http://www.programmingwithbasics.com/
*/
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);
 }
}


See Also:- C Program To Find Greater Number. Among Given Three Number


Output :-

Java Program to Find Greater Among Three Numbers



You May Also See


1. Java Program For Calculator Using AWT Controls (GUI)

2. How To Run Applet/Swing Programs In JAVA Using Command Prompt

3. Java Program For Calculate Percentage Of 5 Subjects

4. Java Program To Find Greater No. Among given Three Number

5. Java Program To Check Number Is Positive Or Negative

No comments:

Post a Comment