Java Code Example: smallest number in an array

This program outputs the smallest number from an array.

import java.util.Scanner;

public class SmallestNumberInArray {
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int[] numbers = new int[20];
		int i = 0, currentNumber, quantity;

		System.out.print("How many numbers do you want to store in array? (1 - 20): " );
		quantity = reader.nextInt();

		// Store numbers in array (user input)
		for(i = 0 ; i < quantity ; i++) {
			System.out.println("Please enter a number: ");
			numbers[i] = reader.nextInt();
		}

		reader.close();
		
		currentNumber = numbers[0];

		// loop over array and store largest number in first array position
		for(i = 1; i < quantity; i++) {
			if(currentNumber > numbers[i]) {
				currentNumber = numbers[i];
			}
		}

		System.out.println("Smallest number in array is: " + currentNumber);
	}
}
Output
How many numbers do you want to store in array? (1 - 20): 5
Please enter a number: 
1
Please enter a number: 
2
Please enter a number: 
3
Please enter a number: 
4
Please enter a number: 
5
Smallest number in array is: 1
Code Explanation

The code defines a Java program to find the smallest number in an array of up to 20 integers.

  1. Importing the Scanner Class: The program imports the java.util.Scanner class to read user inputs from the keyboard.
  2. Main Method: The main method is the starting point of the program. It contains all the instructions that the program will execute.
  3. Creating a Scanner Object: The Scanner object, reader, is created to read input from the keyboard.
  4. Declaring an Array: An array, numbers, of size 20 is declared to store up to 20 numbers.
  5. Reading the Number of Numbers to Store: The program asks the user to enter the number of numbers they want to store in the array using the following line of code:pythonCopy codeSystem.out.print("How many numbers do you want to store in array? (1 - 20): " ); quantity = reader.nextInt();
  6. Reading the Numbers: The program uses a for loop to read the numbers from the user and store them in the array. The following code segment demonstrates this:scssCopy codefor(i = 0 ; i < quantity ; i++) { System.out.println("Please enter a number: "); numbers[i] = reader.nextInt(); }
  7. Closing the Scanner Object: The scanner object is closed using the reader.close() line of code.
  8. Finding the Smallest Number: The program sets the first element of the array as the current number and compares it with the rest of the elements in the array. If an element smaller than the current number is found, the current number is updated with that element. The following code segment demonstrates this:cssCopy codecurrentNumber = numbers[0]; for(i = 1; i < quantity; i++) { if(currentNumber > numbers[i]) { currentNumber = numbers[i]; } }
  9. Displaying the Result: Finally, the program displays the smallest number in the array using the following line of code:goCopy codeSystem.out.println("Smallest number in array is: " + currentNumber);

This program demonstrates how to find the smallest number in an array of integers using Java.