Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Java Code for finding the Smallest Element in an Array

Finding the Smallest element of the array

An array is a data structure which stores the similar type of data. Here we are considering an integer array, which has some elements in it and we will write a Java Program for finding the smallest element of the array

Java Program to find the smallest element in the array(1)

Algorithm for Finding the smallest element of the array

  • Step 1 – Initialize len,min.
  • Step 2 – Accept the length of the array.
  • Step 3 – Initialize an array.
  • Step 4 – Accept the input, using for loop.
  • Step 5 – Store the first element of the array in min.
  • Step 6 – Compare all the elements of the array with min, and use a if condition for comparing all the array elements with min.
  • Step 7 – If there’ll be any element smaller than min, store it value in min.
  • Step 8 – Print min.

JAVA Program for finding the smallest number in an array

import java.util.Scanner;

public class Main
{
public static void main(String args[])
{
int min, len, i;
int arr[] = new int[200];
Scanner scan = new Scanner(System.in);

System.out.print("Enter Length of the array : ");
len = scan.nextInt();

System.out.print("Enter elements in the array : ");
for(i=0; i<len; i++)
{
arr[i] = scan.nextInt();
}

min = arr[0];

for(i=0; i<len; i++)
{
if(min > arr[i])
{
min = arr[i];
}

}

System.out.print("Smallest Element of the array is = " + min);
}
}

Output

Enter the length of the array

5

Enter the elements of the array

5

4

3

2

1

The smallest element of the array is 1