In the previous article, we have seen Java Program to Find the Elements from an Array which are Greater than a Given Number
In this article we are going to see how to Find the Elements from an Array which are Smaller than a Given Number.
Java Program to Find the Elements from an Array which are Smaller than a Given 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 different ways to Find the Elements from an Array which are Smaller than a Given Number.
Method-1: Java Program to Find the Elements from an Array which are Smaller than a Given Number By Static Initialization of Array Elements
Approach:
- Iterate over the array.
- Check if any element is smaller than the given number then print.
Program:
public class Main { public static void main(String[] args) { // initialize the array int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int num = 5; // call the method System.out.print("Elements smaller than " + num + " are: "); findGreater(arr, num); } public static void findGreater(int[] arr, int num) { for (int i : arr) { if (i < num) { System.out.print(i + " "); } } } }
Output: Elements smaller than 5 are: 1 2 3 4
Method-2: Java Program to Find the Elements from an Array which are Smaller than a Given Number By Dynamic Initialization of Array Elements
Approach:
- Create scanner class object.
- Ask use length of the array.
- Initialize the array with given size.
- Ask the user for array elements.
- Iterate over the array.
- Check if any element is smaller than the given number then print.
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { // initialize the array // 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(); } // take input from user for element to be searched System.out.print("Enter the number: "); int num = sc.nextInt(); // call the method System.out.print("Elements smaller than " + num + " are: "); findGreater(arr, num); } public static void findGreater(int[] arr, int num) { // compare each element of array with num for (int i : arr) { if (i < num) { System.out.print(i + " "); } } } }
Output: Enter the size of array: 5 Enter array elements: 1 5 3 2 4 Enter the number: 3 Elements smaller than 3 are: 1 2
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: