Java Program to Find Largest and Smallest Array Number

Write a Java Program to Find the Largest and Smallest Array Number with an example. Or how to write a Program to find the highest and least element or item in a given array.

Java Program to Find Largest and Smallest Array Number using for loop

This Java example uses the Else If statement. The first If statement checks and finds the smallest number, and the else if finds the largest number. Please refer to the Highest Value and Least Value articles.

First, we assigned the array’s first index value to the Smallest and Largest variables. Next, the Else if statement compares each array item against these values. If any other items are less than the Smallest variable, print it as the least value. If any other items are greater than the Largest variable value, print it as the greatest value.

import java.util.Scanner;

public class LargestSmallestArrayItem1 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Size, i, Largest, Smallest, min_Position = 0, max_Position = 0;
		
		sc = new Scanner(System.in);		
		System.out.print("\nPlease Enter the SMLRG Array size = ");
		Size = sc.nextInt();

		int[] smlLrg_arr = new int[Size];	
		System.out.format("\nEnter SMLRG Array %d elements  = ", Size);
		for(i = 0; i < Size; i++) {
			smlLrg_arr[i] = sc.nextInt();
		}
		
		Smallest = smlLrg_arr[0];
		Largest = smlLrg_arr[0];
		
		for(i = 1; i < Size; i++) {
			if(Smallest > smlLrg_arr[i]) 
			{
				Smallest = smlLrg_arr[i];
				min_Position = i;
			}
			else if(Largest < smlLrg_arr[i]) 
			{
				Largest = smlLrg_arr[i];
				max_Position = i;
			}
		}
		System.out.format("\nLargest element in SMLRG Array = %d", Largest);
		System.out.format("\nIndex position of the Largest element = %d", max_Position);
		
		System.out.format("\n\nSmallest element in SMLRG Array = %d", Smallest);
		System.out.format("\nIndex position of the Smallest element = %d", min_Position);
	}
}
Java Program to Find Largest and Smallest Array Number 1

Java Program to Find the Largest and Smallest Array Numbers using functions

In this code example, we created separate functions to find and return the smallest and largest items in the given array.

import java.util.Scanner;

public class LaSmArrItem2 {
	private static Scanner sc;
	static int Size, i, La, Sm, min_Position = 0, max_Position = 0;	
	public static void main(String[] args) {
		
		sc = new Scanner(System.in);		
		System.out.print("\nPlease Enter the SMLRG size = ");
		Size = sc.nextInt();
		
		int[] smlLrg_arr = new int[Size];
		System.out.format("\nEnter SMLRG %d elements  = ", Size);
		for(i = 0; i < Size; i++) {
			smlLrg_arr[i] = sc.nextInt();
		}	
		LaEle(smlLrg_arr);
		SmEle(smlLrg_arr);
	}
	public static void LaEle(int[] lrg_arr ) {
		La = lrg_arr[0];
		for(i = 1; i < Size; i++) {
			if(La < lrg_arr[i]) {
				La = lrg_arr[i];
				max_Position = i;
			}
		}
		System.out.format("\nLargest element in SMLRG = %d", La);
		System.out.format("\nIndex position of it = %d", max_Position);
	}
	public static void SmEle(int[] sm_arr ) {
		Sm = sm_arr[0];
		for(i = 1; i < Size; i++) {
			if(Sm > sm_arr[i]) {
				Sm = sm_arr[i];
				min_Position = i;
			}
		}
		System.out.format("\n\nSmallest element in SMLRG = %d", Sm);
		System.out.format("\nIndex position of it = %d", min_Position);
	}
}

Largest and Smallest Array Item Output.

Java Program to Find Largest and Smallest Array Number using functions