- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Recursive program to find an element in an array linearly.
Following is a Java program to find an element in an array linearly.
Example
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<size; i++){ myArray[i] = s.nextInt(); } System.out.println("Enter the element to be searched: "); int target = s.nextInt(); boolean bool = searchArray(myArray, target ,size); if(bool){ System.out.println("Element found"); }else{ System.out.println("Element not found"); } } }
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
- Related Articles
- Recursive program to linearly search an element in a given array in C++
- Java Program to Recursively Linearly Search an Element in an Array
- Swift Program to Recursively Linearly Search an Element in an Array
- Python Program to find largest element in an array
- Program to find largest element in an array in C++
- Python Program to find the largest element in an array
- PHP program to find the minimum element in an array
- PHP program to find the maximum element in an array
- Golang Program to Find the Largest Element in an Array
- Swift Program to Search an Element in an Array
- C++ Program to Find Largest Element of an Array
- C# program to find the last matching element in an array
- Write a Golang program to find the frequency of an element in an array
- C# Program to find the smallest element from an array
- C# Program to find the largest element from an array

Advertisements