LINEAR SEARCH IN C PROGRAMMING:
Linear search is a searching an algorithm which is used to detect the presence of a number in an array if it is present,it will show its position in the given array.
This algorithm compares each and every element of the array with the search query until the given element is found.
PROGRAM FOR LINEAR SEARCH:
This algorithm compares each and every element of the array with the search query until the given element is found.
PROGRAM FOR LINEAR SEARCH:
#include<stdio.h>
int main()
{
int a[20],i,x,n;
printf("Enter total number of elements?");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("\nEnter element to search:");
scanf("%d",&x);
for(i=0;i<n;++i)
if(a[i]==x)
break;
if(i<n)
printf("Element found in srray %d",i);
else
printf("Element not found");
return 0;
}
OUTPUT:
This will be output for the following program:
Enter total number of elements:7
Enter array elements:12 15 17 19 21 23 25
Enter element to search:19
Element found in array: 4
0 Comments