UncleCoder.com

UncleCoder.com

Free programming examples and instructions

C program to implement linear search

C program to implement linear search

by Krishna viswambharan


Posted on 13 Sep 2017 Category: C Views: 240

Edited on 27 Sep 2017


Linear search is also called as sequential search. This is the simplest searching technique.  Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection.

 The following code implements linear search which is used to find whether a given number is present in an array or not. 

#include <stdio.h>
#include <conio.h>
void main()
{
	int i,n,element,flag=0,num[25];
	printf(“enter the array limit :”);
	scanf(“%d”,&n);
	printf(“\nEnter the array elements :\n”);
	for(i=0;i<n;i++)
	{
		scanf(“%d”,&num[i]);
	}
	printf(“enter the element to be searched :”);
	scanf(“%d”,&element);
	if(num[i]==element)
	{
		flag=1;  //match occures
	}
	if(flag==1)
	{
		printf(“Element is present”);
	}
	else
	{
		printf(“Element not present”);
	}
	getch();
}

output



Leave a Comment:


Advertisement