Monday, May 2, 2016

Linear Search

Write a C code to implement Linear search.



#include<stdio.h>

int main()
{
    int arr[10],n,i,element,found=0;
    printf("Enter The Number of Array Element:\n");
    scanf("%d",&n);
    printf("Enter The %d Array Elements\n",n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    printf("\nElements in Array are:\n");
    for(i=0;i<n;i++)
    {
        printf("%d\t",arr[i]);
    }
    printf("\nEnter Element To Search:\n");
    scanf("%d",&element);
    for(i=0;i<n;i++)
    {
        if(arr[i]==element)
        {
            printf("\nElement %d found at Position %d\n",element,i+1);
            found=1;
        }
    }
    if(found==0)
        printf("\nElement %d Not Found in Array\n",element);
    return 0;
}










Share this

0 Comment to "Linear Search"

Post a Comment