Sunday, February 15, 2015

C program to do linear search in Array

C++ program to do linear search in Array

#include<iostream.h>
#include<conio.h>


void main()
{
 clrscr();
 int a[20],n,x,i,flag=0;

 cout<<"How many Elements?";
 cin>>n;
 cout<<"
Enter Elements of the Array
";


 for(i=0;i<n;++i)
  cin>>a[i];

 cout<<"
Enter Element to search:";
 cin>>x;


 for(i=0;i<n;++i)
 {
  if(a[i]==x)
  {
   flag=1;
   break;
  }
 }

 if(flag)
  cout<<"
Element is Found at position "<<i+1;
 else
  cout<<"
Element not found";
 getch();
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.