Code Competitions

Coding competition
Showing posts with label linear search. Show all posts
Showing posts with label linear search. Show all posts

Monday, 13 May 2013

Program to search element using linear search

#include<stdio.h>
#include<conio.h>
void main()
{
    int a[20],i,j,n,ch,k=0;
    clrscr();
    printf("enter the number of elements");
    scanf("%d",&n);
    printf("enter the elements");
    for(i=0;i<n;i++)
    {
        scanf("\t%d",&a[i]);
    }
    printf("\n");
    for(i=0;i<n;i++)
    {
        printf("\t%d",a[i]);
    }
    printf("\nenter searching elements");
    scanf("%d",&ch);
    for(i=0;i<n;i++)
    {
        if(a[i]==ch)
        {
            printf("\nnumber is present at location %d",i+1);
            k++;
        }
    }
    if(k==0)
    {
    printf("\nnumber is not present");
    }
    else
        printf("\nnumber is present %d times",k);
    getche();
}