Linear Search in Array – C Program

Linear Search in Array – C Program

 Write a C program to search an element in a given array using Liner Search.

#include<stdio.h>
#include<conio.h>

void main()
{

 int a[20],i,value,pos,size;
 printf("Please, Enter total no of elements =");
 scanf("%d", &size);

 for(i=0; i<size; i++)
{
printf("\nThe %d element = ", i+1);
  scanf("%d",&a[i]);
}  

 printf("\nEnter a value to be searched = ");
 scanf("%d",&value);
 
 pos = 0;
 for(i=0; i<size; i++)
 {
  if(a[i]==value)
  {
   pos=i+1;
   break;
  }
 }
 
 if(pos ==0)
{
printf("\n The Value not found in Array");
}
  else
{
 printf("\nThe Value found in Array at Position =%d",pos);
}  

}

The output of linear search in array is :

Please, Enter total no of elements = 10

The 1 element = 21
The 2 element = 25
The 3 element = 95
The 4 element = 76
The 5 element = 72
The 6 element = 55
The 7 element = 46
The 8 element = 85
The 9 element = 10
The 10 element = 66

Enter a value to be searched = 55
The Value found in Array at Position = 6

 

Author: Meera Academy

Leave a Reply

Your email address will not be published. Required fields are marked *