Finding the Smallest and Largest Number from a One-Dimensional Array in Java

One common task in programming is finding the smallest and largest numbers from a given array. In this tutorial post, we will explore how to accomplish this in Java using the BlueJ programming language. We’ll cover the necessary syntax, provide examples, and explain the step-by-step process to find the smallest and largest number from a one-dimensional array.

Creating an Array

To begin, we need an array of numbers. We can create an array and populate it with random or predefined values. Let’s create an array of integers as an example:

int[] numbers = {10, 7, 3, 15, 20};

Finding the Smallest Number

To find the smallest number from the array, we’ll initialize a variable called smallest with the first element of the array. Then, we’ll iterate through the rest of the array, comparing each element with the current value of smallest. If we find a smaller number, we’ll update the value of smallest.At the end of the loop, the variable smallest will hold the smallest number from the array.

int smallest = numbers[0];

for (int i = 1; i < numbers.length; i++)

{
if (numbers[i] < smallest)

{
smallest = numbers[i];
}
}

Finding the Largest Number

Similarly, we can find the largest number from the array by following a similar approach. We’ll initialize a variable called largest with the first element of the array, and then iterate through the rest of the array to update the value of largest if we encounter a larger number.After the loop finishes, the variable largest will hold the largest number from the array.

int largest = numbers[0];

for (int i = 1; i < numbers.length; i++)

{

if (numbers[i] > largest)

{
largest = numbers[i];
}
}

Printing the Results

To verify our results, we can print the smallest and largest numbers to the console.

System.out.println(“Smallest number: ” + smallest);
System.out.println(“Largest number: ” + largest);

Complete code to find the smallest and largest numbers from a one-dimensional array in Java

public class SmallestLargestArray {
public static void main(String[] args) {
int[] numbers = {10, 7, 3, 15, 20};

int smallest = numbers[0];
int largest = numbers[0];

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

if (numbers[i] > largest) {
largest = numbers[i];
}
}

System.out.println(“Smallest number: ” + smallest);
System.out.println(“Largest number: ” + largest);
}

}

Modified code that uses the Scanner class to input array elements from the user

import java.util.Scanner;

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

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

int[] numbers = new int[size];

System.out.println(“Enter the elements of the array:”);

for (int i = 0; i < size; i++) {
numbers[i] = scanner.nextInt();
}

int smallest = numbers[0];
int largest = numbers[0];

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

if (numbers[i] > largest) {
largest = numbers[i];
}
}

System.out.println(“Smallest number: ” + smallest);
System.out.println(“Largest number: ” + largest);
}

}

Conclusion

In this tutorial, we explored how to find the smallest and largest numbers from a one-dimensional array in Java using the BlueJ programming language. By following the step-by-step process and understanding the provided examples, you now have the knowledge to tackle similar problems and apply these techniques to your own Java projects. Happy coding!