Java Program to Find All the Array Elements Which are Smaller than a Specified Number

In the previous article, we have seen Java Program for Mean and Median of an Unsorted Array

In this article we are going to see how to find the array elements which are smaller than a specified number using Java programming language.

Java Program to Find All the Array Elements Which are Smaller than a Specified Number

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see how to find the array elements which are smaller than a specified number.

Method-1: Java Program to Find All the Array Elements Which are Smaller than a Specified Number By Static Initialization of Array Elements

Approach:

  • Declare an array with elements.
  • Declare a number on which you will find the other greater array elements.
  • Iterate the array and check if any array element is smaller than the specified number then print that element.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        //array declared and initialized
        int arr[]={50, 10, 80, 20, 70, 40, 60, 30};
        //a number declared
        //we will find smaller elements than this number in array 
        int num=40;
        
        System.out.println("The array elements which are smaller than the specified number "+num +": ");
        //iterating the array
        for(int i=0;i<arr.length;i++)
        {
            //checking smaller elements than the 'num'
            //and printing those numbers
            if(arr[i]<num)
            {
                System.out.println(arr[i]);
            }
        }
    }
}
Output:

The array elements which are greater than the specified number 40 :
10
20
30

Method-2: Java Program to Find All the Array Elements Which are Smaller than a Specified Number By Dynamic Initialization of Array Elements

Approach:

  • Create the Scanner class object.
  • Ask use length of the array.
  • Initialize the array with given size.
  • Ask the user for array elements.
  • Iterate the array and check if any array element is smaller than the specified number then print that element.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        //Enter a number 
        //we will find smaller elements than this number in array 
        System.out.print("Enter the number: ");
        int num=sc.nextInt();
        
        System.out.println("The array elements which are smaller than the specified number "+num +": ");
        //iterating the array
        for(int i=0;i<arr.length;i++)
        {
            //checking smaller elements than the 'num'
            //and printing those numbers
            if(arr[i]<num)
            {
                System.out.println(arr[i]);
            }
        }
    }
}
Output:

Enter the size of array: 6
Enter array elements: 2 5 1 3 6 4
Enter the number: 6
The array elements which are smaller than the specified number 6: 
2
5
1
3
4

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Related Java Programs: