C++ Program to search an element in an array using function


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


List is :2 3 4 5 10 33 44 55 66

Enter number for search: 44

Number found : 44


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

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

void findNumber(int array[], int size, int num) {

    int f = 0;

    for (int i = 0; i < size; i++) {
        if (num == array[i]) {
            f = 1;
            break;
        }
    }

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

int main() {

    int array[10], i, size, num;

    cout << "Enter Size of an array :";
    cin>>size;

    cout << "\nEnter Array elements :";
    for (i = 0; i < size; i++) {
        cin >> array[i];
    }

    cout << "\nEnter number for search :";
    cin>>num;

    findNumber(array, size, num);

    return 0;
}

Output:

Enter Size of an array :5
Enter Array elements :2 33 11 44 66
Enter number for search :44
Element Found :44