Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Linear Search
- // Date : 17.10.19
- #include<iostream>
- using namespace std;
- int main(){
- int n;
- cout << "Enter the range of array: "; cin >> n;
- int arr[n], i;
- cout << "Enter the element of array: ";
- for(i = 0; i < n; i++){
- cin >> arr[i];
- }
- cout << "Enter the element to search: ";
- int key; cin >> key;
- bool isPresent = false;
- for(i = 0; i < n; i++){
- if(arr[i] == key){
- isPresent = true;
- break;
- }
- }
- if(isPresent){
- cout << "The element " << key << " is present in "
- << i << " position of the array." << endl;
- }
- else{
- cout << "The element " << key
- << " is not present in the array." << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement