Advertisement
fahimkamal63

Linear Search

Oct 18th, 2019
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. //  Linear Search
  2. //  Date : 17.10.19
  3. #include<iostream>
  4. using namespace std;
  5.  
  6. int main(){
  7.     int n;
  8.     cout << "Enter the range of array: "; cin >> n;
  9.     int arr[n], i;
  10.     cout << "Enter the element of array: ";
  11.     for(i = 0; i < n; i++){
  12.         cin >> arr[i];
  13.     }
  14.     cout << "Enter the element to search: ";
  15.     int key; cin >> key;
  16.     bool isPresent = false;
  17.     for(i = 0; i < n; i++){
  18.         if(arr[i] == key){
  19.             isPresent = true;
  20.             break;
  21.         }
  22.     }
  23.     if(isPresent){
  24.         cout << "The element " << key << " is present in "
  25.              << i << " position of the array." << endl;
  26.     }
  27.     else{
  28.         cout << "The element " << key
  29.              << " is not present in the array." << endl;
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement