Java Program to Find Smallest Array Number

Write a Java Program to Find the smallest Array Number with an example or program to print or return the smallest number in a given array.

Java Program to Find Smallest Array Number using for loop

In this smallest array number example, we allowed the user to enter the size and the sm_arr items. Next, we assigned the first array item as the smallest, and next, we compared that value with other items. If any other element is less than that value, we replaced the variable value with that item.

import java.util.Scanner;

public class Simple {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Size, i, Sml, Pos = 0;
		int[] sm_arr = new int[10];
		
		sc = new Scanner(System.in);		
		System.out.print("\n Please Enter the size  : ");
		Size = sc.nextInt();
		
		System.out.format("\nPlease Enter %d Items : ", Size);
		for(i = 0; i < Size; i++) {
			sm_arr[i] = sc.nextInt();
		}
		Sml = sm_arr[0];
		for(i = 1; i < Size; i++) {
			if(Sml > sm_arr[i]) {
				Sml = sm_arr[i];
				Pos = i;
			}
		}
		System.out.format("\nSmallest element in an Array = %d\n", Sml);
		System.out.format("Index position of the Smallest element = %d", Pos);
	}
}
Java Program to find Smallest Array Number 1

Java Program to Find Smallest Array Number using functions

In this example code, we created a separate function that returns the smallest number from the given array. Please refer to the C Program to find the least Number of article to understand the For loop iteration in the Java example.

package ArrayPrograms;

import java.util.Scanner;

public class SmEleUsingMethod {
	private static Scanner sc;
	static int Size, i, Sm, Position = 0;	
	public static void main(String[] args) {
		
		sc = new Scanner(System.in);		
		System.out.print("\nPlease Enter the SM size : ");
		Size = sc.nextInt();

		int[] sm_arr = new int[Size];
		System.out.format("\nEnter SM %d Items :  ", Size);
		for(i = 0; i < Size; i++) {
			sm_arr[i] = sc.nextInt();
		}
		Sm = SmlstEle(sm_arr);
		System.out.format("\nSmallest element in SM = %d", Sm);
	}
	public static int SmlstEle(int[] sm_arr ) {
		Sm = sm_arr[0];
		for(i = 1; i < Size; i++) {
			if(Sm > sm_arr[i]) {
				Sm = sm_arr[i];
				Position = i;
			}
		}
		return Sm; 
	}
}
Please Enter the SM size : 8

Enter SM 8 Items :  10 8 11 22 6 9 22 67

Smallest element in SM = 6

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.