Linear Search
Program on Linear Search
#include<iostream.h>
#include<conio.h>
void main()
{
int a[30],i,n,pos=0,x,flag=0;
clrscr();
cout<<"Enter the number of elements in array: ";
cin>>n;
cout<<"\nEnter the elements in array: ";
for (i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\nEnter the element to be searched: ";
cin>>x;
for(i=0;i<n;i++)
{
if(a[i]==x)
{
pos=i;
flag=1;
break;
}
}
if(flag==0)
{
cout<<"\nUnsuccessful Search";
}
else
{
cout<<"\nThe element is found at position "<<pos+1;
}
getch();
}
Comments
Post a Comment