Linear Search in C

A Linear search or sequential search is a method for finding an element within a list sequentially. It checks each element in the list sequentially until the element found or entire element in the list has been searched.

Below code is the program to find the number using linear search method in C

//LINEAR SEARCH
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[20],n,k,flag=0;
clrscr();
printf("\nEnter the no of terms: ");
scanf("%d",&n);
printf("\n\nEnter the items:\n");
for(i=0;i<n;i++)
  scanf("%d",&a[i]);
printf("\nEnter the item to search:");
scanf("%d",&k);
for(i=0;i<n;i++)
{
 if(a[i]==k)
 {
   printf("\n\nSEARCH FOUND");
   flag=1;
   break;
 }
 else
  flag=0;
}
if(flag==1)
 printf("\n\nThe %d is found at location %d",k,i+1);
else
 printf("\n\nNOT found");
getch();
}

In the above program, the user have to enter the number of terms and then have to enter the items. And lastly the user have to enter the number to search, the code will executed until the number is found and print the location of the number in the list or print the not found message.

Leave a Reply