Java Program to find the largest of three numbers


October 20, 2023, Learn eTutorial
890

Here we are explaining how to write a java program to find out the largest of three numbers. So first we have to read three integer numbers from the user and by using a if else statement we can find out the largest number.

How to implement the java program to find out the largest of three numbers?

First, we have to declare the class LargestNumber.Then we have to declare the variables a,b,c to hold the three integer numbers. Then read the numbers from the user using the scanner class object.By using if condition check if a>b and a>c,if true then display the largest number is a.Else check,b >c.If true then display b as the largest number. Else display c is the largest number.

ALGORITHM

STEP 1: Declare the class LargestNumber with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Declare the integer variables a,b,c.

STEP 4: Create the object of the scanner class as sc.

STEP 5: Read the three numbers into the variables a,b,c.

STEP 6: Check if a>b and a>c, if true then display the largest number is a.

STEP 7: Else check if b> c, if true then display the largest number is b.

STEP 8: Else display the largest number is c.

 

Java Source Code

                                          import java.util.Scanner;
public class LargestNumber 
{
    public static void main(String[] args) 
    {
        int a,b,c;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the first number:");
        a = sc.nextInt();
        System.out.println("Enter the second number:");
        b = sc.nextInt();
        System.out.println("Enter the third number:");
        c = sc.nextInt();
        if(a > b && a > c)
        {
            System.out.println("The largest number is:"+a);
        }
        else if(b > c)
        {
            System.out.println("The largest number is:"+b);
        }
        else
        {
            System.out.println(" The largest number is:"+c);
        }
 
    }
}
                                      

OUTPUT

Enter the first number:8 
Enter the second number:0
Enter the third number:2
The largest number is:8