C Program to Search Element from Array using Recursion

C Program to Search Element from Array using Recursion

Write a Program to Search Element from Array using Recursion

// C Program to Search Element from Array using Recursion

#include <stdio.h>

int searching(int A[],int n, int s, int l)
{

	if(s <= l - 1)
	{
		if(A[s] == n)
		{
			return s;
		}
		else
            return searching(A, n, s + 1, l);
    }
	return -1;
}

int main()
{
	int A[] = {25, 53, 62, 81, 13};
	int n, start = 0, result;
	int l = sizeof(A) / sizeof(A[0]);

    printf("Array Elements :--> ");
    for(int i=0; i<l; i++)
        printf("%d  ", A[i]);

	printf("\nEnter Element to Search :--> ");
	scanf("%d", &n);

	result = searching(A, n, start, l);

	if(result == -1)
		printf("\nElement is not present ");
	else
		printf("\nElement is present at : %d",result);

	return 0;
}

Output:

Run 1:
Array Elements :--> 25  53  62  81  13
Enter Element to Search :--> 81

Element is present at : 3


Run 2:
Array Elements :--> 25  53  62  81  13
Enter Element to Search :--> 11

Element is not present