Write a c program on linear search,Write a c program linear search,Write a c program for linear search
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,pos=-1,ele,n;
clrscr();
printf("How many elements? ");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the search element: ");
scanf("%d",&ele);
for(i=0;i<n;i++)
if(ele==a[i])
{
pos=i;
break;
}
if(pos>=0)
printf("The element %d is present at position %d",ele,pos);
else
printf("The search element %d is not found",ele);
getch();
}
Output:
run 1:
How many elements? 5
Enter the elements
1
2
3
4
5
Enter the search element: 5
The element 5 is present at position 4
run 2:
How many elements? 5
Enter the elements
1
2
3
4
5
Enter the search element: 6
The search element 6 is not found
No comments: