ADVERTISEMENT
ADVERTISEMENT

Java program to find Largest of Three Numbers

Largest of Three Numbers in Java - This program will read three integer numbers from the user and find the largest number among them, here we will find the largest number using if else conditions, ternary operator and function/method.

Largest of Three Numbers using Java program

//Java program to find Largest of three numbers.
 
import java.util.*;
 
public class LargestNumber{   
     
     public static void main(String []args)
     {
            int a=0,b=0,c=0;
            int largest=0;
            //Scanner class to take user input.
            Scanner X = new Scanner(System.in);
             
            System.out.print("Enter First No. :");
            a = X.nextInt(); //read integer number
             
            System.out.print("Enter Second No. :");
            b = X.nextInt(); //read integer number
             
            System.out.print("Enter Third No. :");
            c = X.nextInt(); //read integer number
             
            if( a>b && a> c)
                largest = a;
            else if(b>a && b>c)
                largest = b;
            else
                largest = c;
             
            System.out.println("Lagest Number is : "+largest);
     }
}

Output

    
    [email protected]:~$ javac LargestNumber.java
    [email protected]:~$ java LargestNumber

    Enter First No. :23
    Enter Second No. :100
    Enter Third No. :12
    Lagest Number is : 100

Using Ternary Operator

    largest = (a>b && a>c)?a:(b>c&&b>c)?b:c;
ADVERTISEMENT

Using Function/Method

import java.util.*;
 
public class LargestNumber{
     
    //Funtion to find largest number among three numbers
    //Using if statement.
    static int findLargest(int a,int b, int c)
    {
        if( a>b && a> c)
            return a;
        else if(b>a && b>c)
            return b;
        else
            return c;
    }
     public static void main(String []args)
     {
            int a=0,b=0,c=0;
             
            //Scanner class to take user input.
            Scanner X = new Scanner(System.in);
             
            System.out.print("Enter First No. :");
            a = X.nextInt(); //read integer number
             
            System.out.print("Enter Second No. :");
            b = X.nextInt(); //read integer number
             
            System.out.print("Enter Third No. :");
            c = X.nextInt(); //read integer number
             
            System.out.println("Lagest Number is : "+findLargest(a,b,c));
     }
}

Core Java Example Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.