Java program to find the largest element in an array | Java FAQ Solution

Finding the largest element in an array is a common task that helps to understand array manipulation and traversal in Java. Below is a detailed Java program to find the largest element in an array – Java program to find the largest element in an array.

Java program to find the largest element in an array
Java program to find the largest element in an array

Java Program to Find the Largest Element in an Array

import java.util.Scanner;

public class LargestElementInArray {
    public static void main(String[] args) {
        // Create a Scanner object to read input from the user
        Scanner scanner = new Scanner(System.in);

        // Prompt the user to enter the size of the array
        System.out.print("Enter the size of the array: ");
        int size = scanner.nextInt();

        // Create an array of the given size
        int[] array = new int[size];

        // Prompt the user to enter the elements of the array
        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < size; i++) {
            array[i] = scanner.nextInt();
        }

        // Find the largest element in the array
        int largest = findLargestElement(array);

        // Print the largest element
        System.out.println("The largest element in the array is: " + largest);
    }

    // Method to find the largest element in an array
    public static int findLargestElement(int[] array) {
        // Assume the first element is the largest
        int largest = array[0];

        // Traverse through the array to find the largest element
        for (int i = 1; i < array.length; i++) {
            if (array[i] > largest) {
                largest = array[i];
            }
        }

        return largest;
    }
}

Top 100 Java Programs: Click Here

Are you looking for a job: Click Here

Explanation of the Code: (Java program to find the largest element in an array. )

Let’s break down the program step-by-step to understand how it works.

  1. Importing the Scanner Class: We import the Scanner class from the java.util package to read input from the user.
import java.util.Scanner;

2. Creating the Main Class: We create a public class named LargestElementInArray. In Java, the class name should match the filename.

public class LargestElementInArray {

3. Main Method: The main method is the entry point of the program. Inside this method, we will write the code to find the largest element in an array.

public static void main(String[] args) {

4. Creating a Scanner Object: We create a Scanner object to read input from the user.

Scanner scanner = new Scanner(System.in);

5. Reading the Size of the Array: We prompt the user to enter the size of the array and store this value in a variable.

System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();

6. Creating an Array: We create an array of the given size.

int[] array = new int[size];

7. Reading the Elements of the Array: We prompt the user to enter the elements of the array and store these values in the array.

System.out.println("Enter the elements of the array:");
for (int i = 0; i < size; i++) {
    array[i] = scanner.nextInt();
}

8. Finding the Largest Element: We call the findLargestElement method to find the largest element in the array. The result is stored in a variable largest.

int largest = findLargestElement(array);

9. Printing the Largest Element: We print the largest element in the array.

System.out.println("The largest element in the array is: " + largest);

10. Method to Find the Largest Element in an Array: The findLargestElement method finds the largest element in the given array. This method works by assuming the first element is the largest and then traversing through the array to find the actual largest element.

public static int findLargestElement(int[] array) {
    // Assume the first element is the largest
    int largest = array[0];

    // Traverse through the array to find the largest element
    for (int i = 1; i < array.length; i++) {
        if (array[i] > largest) {
            largest = array[i];
        }
    }

    return largest;
}

Explanation of the findLargestElement Method

  1. Initializing the Largest Element: We initialize the largest variable with the first element of the array.
int largest = array[0];

2. Traversing the Array: The for loop starts from the second element (index 1) and goes to the end of the array. In each iteration, we compare the current element with the largest variable. If the current element is larger, we update the largest variable.

for (int i = 1; i < array.length; i++) {
    if (array[i] > largest) {
        largest = array[i];
    }
}

3. Returning the Largest Element: After the loop finishes, we return the largest variable, which now holds the largest element in the array.

return largest;

Running the Program

To run the program, follow these steps:

  1. Save the code in a file named LargestElementInArray.java.
  2. Open a command prompt or terminal and navigate to the directory where you saved the file.
  3. Compile the program using the following command:
javac LargestElementInArray.java

4. Run the compiled program using the following command:

java LargestElementInArray
  1. Follow the prompts to enter the size and elements of the array. The program will display the largest element in the array.

Example Output

If you run the program and enter the following input:

Enter the size of the array: 5
Enter the elements of the array:
10
45
32
67
29

The output will be:

The largest element in the array is: 67

What did we learn from this article?

Finding the largest element in an array is a fundamental exercise that helps in understanding array manipulation and traversal in Java. This Java program demonstrates how to find the largest element in an array by iterating through the array and comparing each element to find the maximum. By practicing such basic programs, you can strengthen your problem-solving skills and gain confidence in your ability to write efficient Java code. Keep practicing and exploring more problems to enhance your programming skills.

Leave a Comment