C++ Program to search an element in the 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 a number for search:60

The number is found in list:60

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

#include<iostream>
using namespace std;

int main() {

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

    cout << "Enter 5 numbers:";
    for (i = 0; i < 5; i++) {
        cin >> arr[i];
    }
    cout << "Enter number for search:";
    cin>>num;

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

    if (f == 1) {
        cout << "Number found:" << num;
    } else {
        cout << "Number not found:" << num;
    }

    return 0;
}

Output:

Enter 5 numbers:10 20 30 40 50
Enter number for search:30
Number found:30