Program to implement Linear Search in the array

/* Linear Search in the array

search the value in serial way (one after another ) */

/* here a[100] – storage of array value 

f,l,m    – first, last, mid position of array

x    – search value

i    – index variable

flag – determine the value found or not found

*/

#include<stdio.h>

#include<conio.h>

void main()

{

int a[100],i,n,x,flag=0;

clrscr();

printf(“enter how many value in array\n”);

scanf(“%d”,&n);

printf(“Enter %d value \n”,n);

for(i=0;i<n;i++)

scanf(“%d”,&a[i]);

printf(“Which value to be search ->”);

scanf(“%d”,&x);

/* Linear Search logic */

for(i=0;i<n;i++)

if(x==a[i])

{

flag=1;

break;

}

if(flag==0)

printf(“%d value not found\n”,x);

else

printf(“%d value found at location %d\n”,x,i);

getch();

}

 

Mohit Arora
Follow me