Search an element in array - C++ Program

Q. Write a C++ program to search any element in an array.

Answer:

Following program performs the search operation to find the elements in an Array.

#include<iostream>
using namespace std;

int main()
{
        int arr[10], i, num, n, cnt=0, pos;
        cout<<"\n Enter Array Size : ";
        cin>>n;
        cout<<"\n Enter Array Elements : \n";
        for(i=0; i<n; i++)
        {
                cout<<" ";
                cin>>arr[i];
        }
        cout<<"\n Enter Element to be Searched : ";
        cin>>num;
        for(i=0; i<n; i++)
        {
                if(arr[i]==num)
                {
                        cnt=1;
                        pos=i+1;
                        break;
                }
        }
        if(cnt==0)
        {
                cout<<"\n Element Not Found..!!";
        }
        else
        {
                cout<<"\n Element "<<num<<" Found At Position "<<pos;
        }
        return 0;
}


Output:

search array element

search array element