DSA/searching/c-or-cpp/linear-search.cpp

36 lines
721 B
C++
Raw Normal View History

2020-12-16 14:08:13 +00:00
#include<iostream> //Header Files
using namespace std;
int main()
{
int array[100];
int i;
int key;
int n;
int pos =0;
cout<<"please enter the elements of array \n";
cin>>n;
cout<<"please enter the array elements \n";
for (i=0;i<n;++i)
cin>>array[i];
cout<<"the elements of the array are \n";
for (i=0;i<n;++i)
cout<<array[i]<<" ";
cout<<"\n";
cout<<"please enter the number which you want to find in the inputted array \n";
2020-12-16 14:08:13 +00:00
cin>>key;
cout<<"the inputted no is at position \n";
2020-12-16 14:08:13 +00:00
for(i=0;i<n;++i)
{
if(key==array[i]&&i<n)
cout<<i+1<<"\n";
}
}