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.

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.

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 Largest and Smallest Array Number Example 2

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

Please Enter the SMLRG size = 10

Enter SMLRG 10 elements  = 9 11 6 55 77 9 5 2 22 65

Largest element in SMLRG = 77
Index position of it = 4

Smallest element in SMLRG = 2
Index position of it = 7