Home »
Java Programs »
Core Java Example Programs
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
me@linux:~$ javac LargestNumber.java
me@linux:~$ 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;
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 »