Linear Search in C

Here you will discover a program for direct search in C.

Straight search is the most straightforward looking through calculation which is here and there known as successive hunt. In this calculation, every component of the cluster is contrasted and the focused on component successively.

Linear Search in C

#include<stdio.h>
 
int main()
{
	int a[20],i,x,n;
	printf("How many 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 at index %d",i);
	else
		printf("Element not found");
 
	return 0;
}

Output 

How many elements?4
Enter array elements:
6 8 9 1

Enter element to search:9
Element found at index 2

Leave a Comment

error: Alert: Content is protected!!