C++ Program to Find an Element from Array

C++ Program to Find an Element from Array

Write C++ Program to Find an Element from Array

// CPP Program to Find an Element from Array

#include <iostream>

using namespace std;

int main()
{
	int A[] = {1, 2, 3, 4, 5}, n, i, flag=0;

	cout << "The Array :--> ";
	for(i=0; i<5; i++)
    {
        cout << A[i] << " ";
    }

	cout << "\nEnter Element to Search from Array :--> ";
	cin >> n;

	for(i = 0; i < 5; i++)
	{
		if(A[i] == n)
		{
			cout << "\nThe element is present at index " << i;
			flag = 1;
			break;
		}

	}
	if(flag == 0)
	{
		cout << "\nThe element is not present in the array";
	}
	return 0;
}

Output:

Run 1:
The Array :--> 1  2  3  4  5
Enter Element to Search from Array :--> 2
The element is present at index 1


Run 2:
The Array :--> 1  2  3  4  5
Enter Element to Search from Array :--> 22
The element is not present in the array