Java program to find the smallest element in an array

Finding the smallest element in an array is another fundamental task that helps understand array manipulation and traversal in Java. Below is a detailed Java program to find the smallest element in an array.

Java Program to Find the Smallest Element in an Array

import java.util.Scanner;

public class SmallestElementInArray {
    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 smallest element in the array
        int smallest = findSmallestElement(array);

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

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

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

        return smallest;
    }
}
Java program to find the smallest element in an array

Explanation of the Code

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 SmallestElementInArray. In Java, the class name should match the filename.

public class SmallestElementInArray {

3. Main Method: The main method is the entry point of the program. Inside this method, we will write the code to find the smallest 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 Smallest Element: We call the findSmallestElement method to find the smallest element in the array. The result is stored in a variable smallest.

int smallest = findSmallestElement(array);

9. Printing the Smallest Element: We print the smallest element in the array.

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

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

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

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

    return smallest;
}

Explanation of the findSmallestElement Method

  1. Initializing the Smallest Element: We initialize the smallest variable with the first element of the array.
int smallest = 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 smallest variable. If the current element is smaller, we update the smallest variable.

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

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

return smallest;

Running the Program

To run the program, follow these steps:

  1. Save the code in a file named SmallestElementInArray.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 SmallestElementInArray.java

4. Run the compiled program using the following command:

java SmallestElementInArray
  1. Follow the prompts to enter the size and elements of the array. The program will display the smallest 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 smallest element in the array is: 10

What did we learn from this article?

Finding the smallest 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 smallest element in an array by iterating through the array and comparing each element to find the minimum. 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