Linear search using recursion


#include<stdio.h> 
int lSearch(int arr[],  int search,int index,int n); 
int main() 
{ 
    int n, search, result, m = 0, arr[100]; 
    printf("Program on Linear Search\n");
    printf("Enter the total elements in the array\n"); 
    scanf("%d", &n); 
    printf("Enter the array elements\n"); 
    for (int i = 0; i < n; i++) 
    { 
        scanf("%d", &arr[i]); 
    } 
    printf("Enter the element to search \n"); 
    scanf("%d", &search); 
  result =  lSearch(arr,  search,0,n); 
    if (result != 0) 
    { 
        printf("Element found at pos %d\n ", result); 
    } 
    else 
    { 
        printf("Element not found"); 
    } 
return 0; 
} 
int lSearch(int arr[],  int search,int index,int n) 
{ 
    int arrpos=0; 
    if(index>=n) 
    { 
        return 0; 
    } 
        if (arr[index] == search) 
        { 
            arrpos = index + 1; 
            return arrpos; 
             
        } 
        else 
        { 
        return lSearch(arr, search,index+1,n); 
        } 
    return arrpos; 
} 

Comments

Popular Posts