Java Program to Find Largest Number in an Array

Introduction

Finding the largest number in an array is a common programming task, often used in data analysis and processing. This guide will show you how to create a Java program that identifies and displays the largest number in a given array.

Problem Statement

Create a Java program that:

  • Takes an array of integers as input.
  • Finds and displays the largest number in the array.

Example 1:

  • Input: {3, 5, 7, 2, 8}
  • Output: The largest number is 8

Example 2:

  • Input: {12, 45, 7, 89, 34}
  • Output: The largest number is 89

Solution Steps

  1. Initialize the Array: Define an array with a set of integer values.
  2. Initialize a Variable for the Largest Number: Set a variable to hold the largest number, initialized to the smallest possible integer value.
  3. Loop Through the Array: Iterate through the array and compare each element to the current largest number.
  4. Update the Largest Number: If an element is greater than the current largest number, update the largest number.
  5. Display the Largest Number: Print the largest number found in the array.

Java Program

Approach: Using a for Loop

import java.util.Scanner;

/**
 * Java Program to Find the Largest Number in an Array using a for loop
 * Author: https://www.javaguides.net/
 */
public class LargestNumberInArray {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Step 1: Initialize the array
        System.out.print("Enter the number of elements in the array: ");
        int n = scanner.nextInt();
        int[] array = new int[n];

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

        // Step 2: Initialize the largest number variable
        int largest = array[0];

        // Step 3: Loop through the array to find the largest number
        for (int i = 1; i < array.length; i++) {
            if (array[i] > largest) {
                largest = array[i];
            }
        }

        // Step 4: Display the largest number
        System.out.println("The largest number is " + largest);
    }
}

Explanation

  • Input: The program prompts the user to enter the number of elements in the array and the elements themselves.
  • Finding the Largest Number: The program initializes the largest variable with the first element of the array. It then iterates through the array, comparing each element with the current largest value. If a larger value is found, the largest variable is updated.
  • Output: The program prints the largest number found in the array.

Output Example

Enter the number of elements in the array: 5
Enter the elements of the array:
3 5 7 2 8
The largest number is 8

Conclusion

This Java program efficiently finds the largest number in an array by iterating through the array and updating a variable that holds the largest value found so far. This method is straightforward and works well for finding the maximum value in a list of numbers, making it a useful tool in various data processing tasks.

Comments