Saturday, 26 April 2014

Linear Search


PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
    int a[10],i,n,key,f=0;
    clrscr();
    printf("\n\n LINEAR SEARCH \n\n");
    printf("Enter the array limit:\t");
    scanf("%d",&n);
    printf("Enter array elements:\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("Enter the element to be searched:\t");
    scanf("%d",&key);
    for(i=0;i<n;i++)
    {
        if(a[i]==key)
        {
            printf("Element found\n");
            f=1;
            break;
        }
    }
    if(f==0)
    {
        printf("Element not found\n");
    }
    getch();
}


OUTPUT:


LINEAR SEARCH

Enter the array limit:  5
Enter array elements:
1
3
5
7
9
Enter the element to be searched:       9
Element found



LINEAR SEARCH

Enter the array limit:  5
Enter array elements:
2
4
6
8
10
Enter the element to be searched:       9
Element not found

No comments:

Post a Comment