Find Element in an Array Linearly Using Recursion



Following is a Java program to find an element in an array linearly.

Example

 Live Demo

import java.util.Scanner;
public class SearchingRecursively {
   public static boolean searchArray(int[] myArray, int element, int size){
      if (size == 0){
         return false;
      }
      if (myArray[size-1] == element){
         return true;
      }
      return searchArray(myArray, element, size-1);
   }
   public static void main(String args[]){
      System.out.println("Enter the required size of the array: ");
      Scanner s = new Scanner(System.in);
      int size = s.nextInt();
      int myArray[] = new int [size];
      System.out.println("Enter the elements of the array one by one ");
      for(int i=0; i

Output

Enter the required size of the array:
5
Enter the elements of the array one by one
14
632
88
98
75
Enter the element to be searched:
632
Element found
Updated on: 2019-07-30T22:30:26+05:30

863 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements