Monday, 8 January 2018

Linear Search program in C

//program for linear search
#include<stdio.h>
void main()
{
int a[10],n,i,search,flag=0;
clrscr();
printf("How many numbers:");
scanf("%d",&n);
printf("\nEnter the numbers :");
for(i=0;i<n;i++)
 scanf("%d",&a[i]);
printf("\nEnter the element to be searched:");
scanf("%d",&search); //reading search element
 for(i=0;i<n;i++)
 {
  if(a[i]==search) // comparing search element to the
   { //element in the array list.
     flag=1;
     break; //if element is found make flag=1
   }
 }
 if(flag==1) //if flag=1 it means element found
  printf("\nElement found at position %d",i+1);
 else //i+! is the element position
  printf("\nElement Not Found");
getch();
}


No comments:

Post a Comment