C++ Program to search an element in array


In this program, You will learn how to search an element in array in C++.


List is: 10 20 30 40 50 60

Enter number for search :60

Number is found in list :60


Example: How to search an element in array in C++

Run it Now
#include<iostream>
using namespace std;

int main() {

    int i, num, f = 0;
    int arr[10];

    cout << "Enter 10 Numbers :";
    for (i = 0; i < 10; i++) {
        cin >> arr[i];
    }
    cout << "\nEnter Number For Search :";
    cin>>num;

    for (i = 0; i < 10; i++) {
        if (num == arr[i]) {
            f = 1;
            break;
        }
    }

    if (f == 1) {
        cout << "\nNumber Found :" << num;
    } else {
        cout << "\nNumber Not Found :" << num;
    }

    return 0;
}

Output:

Enter 10 Numbers :1 2 3 4 5 6 7 8 9 10
Enter Number For Search :4
Number Found :4