codescracker


c

C Program Linear Search



« Previous Program Next Program »


Linear Search in C

To search any element present inside the array in C programming using linear search technique, you have to use only one for loop as shown in the following program.

C Programming Code for Linear Search

Following C program first ask to the user to enter the array size then it will ask to enter the array elements, then it will finally ask to enter a number or element to be search in the given array to check whether it is present in the array or not, if it is present then at which position :

/* C Program - Linear Search */
		
#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	int arr[10], i, num, n, c=0, pos;
	printf("Enter the array size : ");
	scanf("%d",&n);
	printf("Enter Array Elements : ");
	for(i=0; i<n; i++)
	{
		scanf("%d",&arr[i]);
	}
	printf("Enter the number to be search : ");
	scanf("%d",&num);
	for(i=0; i<n; i++)
	{
		if(arr[i]==num)
		{
			c=1;
			pos=i+1;
			break;
		}
	}
	if(c==0)
	{
		printf("Number not found..!!");
	}
	else
	{
		printf("%d found at position %d",num, pos);
	}
	getch();
}

When the above c program is compile and executed, it will produce the following result:

c program linear search

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test