Program to Search Element From Array Using Recursion

Program to Search Element From Array Using Recursion

  • Write a program to Search Element From Array Using Recursion in C
  • Write a program to Search Element From Array Using Recursion in C++
  • Write a program to Search Element From Array Using Recursion in Python
  • Write a program to Search Element From Array Using Recursion in PHP
  • Write a program to Search Element From Array Using Recursion in Java
  • Write a program to Search Element From Array Using Recursion in Java Script
  • Write a program to Search Element From Array Using Recursion in C#

Explanation:

Recursion is a technique for finding an element in an array by comparing the current element with the target value and moving on to the next element if they don’t match. Until the target is located or the array is completely explored, the procedure keeps going.

Recursive Algorithm:

  1. Define a function search(arr, key, index):
    • arr is the array.
    • key is the element to search for.
    • index is the current position in the array.
  2. Base Case:
    • If index == n, return -1 (element not found).
    • If arr[index] == key, return index.
  3. Recursive Case:
    • Call the function for the next index: search(arr, key, index + 1).

Program to Search Element From Array Using Recursion

#include <stdio.h>

int searchElement(int arr[], int n, int key) {
    if (n == 0) {
        return -1;
    }
    if (arr[n - 1] == key) {
        return n - 1;
    }
    return searchElement(arr, n - 1, key);
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    int key;

    printf("Enter the element to search: ");
    scanf("%d", &key);

    int result = searchElement(arr, n, key);
    if (result != -1) {
        printf("Element found at index %d\n", result);
    } else {
        printf("Element not found\n");
    }
    return 0;
}

#include <iostream>
using namespace std;

int searchElement(int arr[], int n, int key) {
    if (n == 0) {
        return -1;
    }
    if (arr[n - 1] == key) {
        return n - 1;
    }
    return searchElement(arr, n - 1, key);
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    int key;

    cout << "Enter the element to search: ";
    cin >> key;

    int result = searchElement(arr, n, key);
    if (result != -1) {
        cout << "Element found at index " << result << endl;
    } else {
        cout << "Element not found" << endl;
    }
    return 0;
}

def search_element(arr, n, key):
    if n == 0:
        return -1
    if arr[n - 1] == key:
        return n - 1
    return search_element(arr, n - 1, key)

arr = [1, 2, 3, 4, 5]
key = int(input("Enter the element to search: "))
result = search_element(arr, len(arr), key)

if result != -1:
    print(f"Element found at index {result}")
else:
    print("Element not found")

<?php
function searchElement($arr, $n, $key) {
    if ($n == 0) {
        return -1;
    }
    if ($arr[$n - 1] == $key) {
        return $n - 1;
    }
    return searchElement($arr, $n - 1, $key);
}

$arr = array(1, 2, 3, 4, 5);
echo "Enter the element to search: ";
$key = intval(trim(fgets(STDIN)));

$result = searchElement($arr, count($arr), $key);
if ($result != -1) {
    echo "Element found at index $result\n";
} else {
    echo "Element not found\n";
}
?>

import java.util.Scanner;

public class Main {
    public static int searchElement(int[] arr, int n, int key) {
        if (n == 0) {
            return -1;
        }
        if (arr[n - 1] == key) {
            return n - 1;
        }
        return searchElement(arr, n - 1, key);
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the element to search: ");
        int key = scanner.nextInt();

        int result = searchElement(arr, arr.length, key);
        if (result != -1) {
            System.out.println("Element found at index " + result);
        } else {
            System.out.println("Element not found");
        }
    }
}

function searchElement(arr, n, key) {
    if (n === 0) {
        return -1;
    }
    if (arr[n - 1] === key) {
        return n - 1;
    }
    return searchElement(arr, n - 1, key);
}

const arr = [1, 2, 3, 4, 5];
const key = parseInt(prompt("Enter the element to search:"));
const result = searchElement(arr, arr.length, key);

if (result !== -1) {
    console.log(`Element found at index ${result}`);
} else {
    console.log("Element not found");
}

using System;

class Program {
    static int SearchElement(int[] arr, int n, int key) {
        if (n == 0) {
            return -1;
        }
        if (arr[n - 1] == key) {
            return n - 1;
        }
        return SearchElement(arr, n - 1, key);
    }

    static void Main() {
        int[] arr = {1, 2, 3, 4, 5};
        Console.Write("Enter the element to search: ");
        int key = int.Parse(Console.ReadLine());

        int result = SearchElement(arr, arr.Length, key);
        if (result != -1) {
            Console.WriteLine("Element found at index " + result);
        } else {
            Console.WriteLine("Element not found");
        }
    }
}

List of All Programs