Write a program to search an element is a given list of values

Previous
Next

Linear Search:

Linear search is one of searching algorithms to find an element in array. Linear search can apply on random array also. In linear search, every time index element compare with the element to be searched. If element found, it returns the index value as location. If element not found, it display error message.

#include<stdio.h>
int main()
{
	int a[30], m, key, i, n, found=0;
	printf("Enter size of array : ");
	scanf("%d",&m);

	printf("Enter Elements in array : \n");
	for(i=0; i<m; i++)
		scanf("%d",&a[i]);

	printf("Enter the key to be searched: \n");
	scanf("%d",&key);
	
	for(i=0; i<n; i++)
	{
		if(key==a[i])
		{
			found=1;
			printf("Found @ location : %d \n", i);
			break;
		}
	}
	if(!found)
		printf("Element not found \n");
	return 0;
}
Previous
Next

Courses Enquiry Form