linear search in c++

 Linear serach is c++

code:-

#include<iostream>
using namespace std;
int main()
{
    int i,search,count=0,n;
    cout<<"Enter the array size"<<endl;
    cin>>n;
    cout<<"enter the array elements"<<endl;
    int arr[n];
    for(i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    cout<<"enter the element to be searched"<<endl;
    cin>>search;
    for ( i = 0; i <n; i++)
    {
        if (arr[i]==search)
        {
            cout<<search<<" is present at the location "<<(i+1)<<endl;
            count++;
            /* code */
        }
       
        /* code */
    }
    if(count==0)
    {
        cout<<search <<" is not found anywhere"<<endl;
    }
    else
    {
        cout<<search<<" is present "<<count<<" times in the array"<<endl;
    }
    return 0;
   
   
   
}

Comments

Popular Posts