C program for Linear search in array
Linear Search in C
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 :
Source Code:
/* 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();
}

No comments:
Post a Comment