Linear Search in C++

C++ Program to perform Linear Search, Linear Search basically iterates from start to end in order to find the key element.

Source code :


#include<iostream>

using namespace std;

int main(){
    
    
    int search_array[] = {4,1,56,7,10,71,63,17,2,3};
    
    int key = 17; //Element to search
    
    int i, size = sizeof(search_array)/sizeof(search_array[0]);
    int found = 0, position;

    for(i = 0 ; i < size; i++){
        if(search_array[i] == key){
            found = 1;
            position = i;
            break;
        }
    }
    
    
    if(found){
        cout<<"Element "<<key<<" found at position "<<position;
    }else{
        cout<<"Element not found";
    }
    

    return 0;

}

Output :

Element 17 found at position 7

Output :


                

Comments :