Tuesday, 19 November 2013

linear search without recursion

Linear Search Without Recursion Using C Program

Program:


#include<stdio.h>

void main()

{

    int a[50],n,f,key,i;

    clrscr();

    printf("enter no of elements to be entered");

    scanf("%d",&n);

    printf("enter elements ");

    for(i=1;i<=n;i++)

        scanf("%d",&a[i]);

    printf("enter the key value to find");

    scanf("%d",&key);

    f=lsearch(a,n,key);

    if(f==0)

       printf("the key not found");

    else

       printf("the %d is found at %d",key,f);

    getch();

}

int lsearch(int a[],int n,int key)

{

    int i;

    for(i=1;i<=n;i++)

    {

        if(a[i]==key)

        return i;

    }

    return 0;

}

Output:


No comments:

Post a Comment