Navigation

Saturday, 28 February 2015

PROGRAM FOR LINEAR SEARCH

 /*PROGRAM FOR LINEAR SEARCH*/
void main()
{
    int n,i,key,a[20];
    clrscr();
    printf("............PROGRAM FOR LINEAR SEARCH..............\n\n\n");
    printf("Enter the number of elements you want to insert in the array::=>\n");
    scanf("%d",&n);
    printf("enter the %d elements in the array\n",n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("Enter the element you want to search\n");
    scanf("%d",&key);
    for(i=0;i<n;i++)
    {
        if(a[i]==key)
        {
            printf("element has been found\n\n");
            printf("Position of element is %d",i+1);
        }
        if(a[i]==n)
            printf("element has not been found");
    }

    getch();
}

No comments:

Post a Comment