w3resource

Java Project to find the Largest Number

Find the Largest Number using a For Loop and Arrays Class:

Find the Largest Number:

Input: An array or list of numbers.
Output: The largest number in the list.

Example:

  • Input: [12, 45, 3, 89, 22]
  • Output: "Largest number: 89"
  • Input: [7, 2, 9, 5]
  • Output: "Largest number: 9"

Solution 1: Find the Largest number using a For Loop

Code:

import java.util.Scanner;

public class LargestNumber {
    // Method to find the largest number in an array using a for loop
    public static int findLargest(int[] numbers) {
        int max = numbers[0];  // Assume the first number is the largest

        // Loop through the array to find the largest number
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] > max) {
                max = numbers[i];  // Update max if current number is larger
            }
        }
        return max;  // Return the largest number found
    }

    // Main method to take user input and display the largest number
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Take the size of the array as input
        System.out.print("Enter the number of elements: ");
        int size = scanner.nextInt();

        int[] numbers = new int[size];

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

        // Find and display the largest number
        int largest = findLargest(numbers);
        System.out.println("Largest number: " + largest);
        
        scanner.close();  // Close the scanner
    }
}   

Output:

Enter the number of elements:  5
Enter the numbers: 
 23
 23
 4564
 4565
 23423
Largest number: 23423

Explanation :

  • Input: User inputs the size of the array and the numbers.
  • Processing: The program assumes the first number is the largest, then iterates through the array, updating the largest value if a bigger number is found.
  • Output: The largest number is displayed.

Solution 2: Find the Largest number using Java Arrays Class

Code:

import java.util.Arrays;
import java.util.Scanner;

public class LargestNumberWithArrays {
    // Method to find the largest number using Arrays.sort() method
    public static int findLargest(int[] numbers) {
        Arrays.sort(numbers);  // Sort the array in ascending order
        return numbers[numbers.length - 1];  // Return the last element (largest)
    }

    // Main method to take user input and display the largest number
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Take the size of the array as input
        System.out.print("Enter the number of elements: ");
        int size = scanner.nextInt();

        int[] numbers = new int[size];

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

        // Find and display the largest number
        int largest = findLargest(numbers);
        System.out.println("Largest number: " + largest);
        
        scanner.close();  // Close the scanner
    }
}

Output:

Enter the number of elements:  7
Enter the numbers: 
 12
 33
 78
 -34
 0
 63
 45
Largest number: 78

Explanation:

  • Input: User enters the size of the array and the elements.
  • Processing: The Arrays.sort() method sorts the numbers, and the largest number is the last element in the sorted array.
  • Output: The largest number is printed.

Java Code Editor:




Follow us on Facebook and Twitter for latest update.