Here is the C Code for Linear Search:
/* Double-Click To Select Code */
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10],num,i,n,found=0,pos=-1;
clrscr();
printf("Enter the number of elements in the array: ");
scanf("%d",&n);
printf("\nEnter the elements: \n");
for(i=0;i<n;i++)
{
printf("arr[%d] = ",i);
scanf("%d",&arr[i]);
}
printf("\nEnter the number that has to be searched: ");
scanf("%d",&num);
for(i=0;i<n;i++)
{
if(arr[i]==num)
{
found=1;
pos=i;
printf("\n %d is found in the array at position = %d",num,i);
break;
}
}
if(found==0)
printf("\ %d does not exist in the array",num);
getch();
}
Output:
Please Comment If You Liked This Post !!
This article uses material from the Wikipedia article Linear Search which is released under the Creative Commons Attribution-Share-Alike License 3.0
Do you like this post? Please link back to this article by copying one of the codes below.
URL: HTML link code: Forum link code:
