Program to implement Linear Search in CPP (C plus plus)
In this tutorial, we will learn about Program to implement Linear Search in CPP (C plus plus).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include<iostream> using namespace std; int main() { int array[20]; int val; int k; int i; int j=0; cout<<"Please enter the number of values you want to enter: "; cin>>val; cout<<endl<<"Please enter the values into the array"<<endl; for(i=0;i<val;++i) cin>>array[i]; cout<<endl<<"Please enter value to search: "; cin>>k; for(i=0;i<val;++i) { if(array[i]==k) { j=1; break; } } if(j) cout<<endl<<"Value found at position "<<i+1; else cout<<endl<<"Value not found"; return 0; } |
Output:
