हिंदी

Write a C++ program to input ‘n’ elements in an one dimensional array and input one another number and check whether this number is present in the array or not. - Computer Science 1

Advertisements
Advertisements

प्रश्न

Write a C++ program to input ‘n’ elements in an one dimensional array and input one another number and check whether this number is present in the array or not.

कोड लेखन
विस्तार में उत्तर
Advertisements

उत्तर

#include <iostream>
using namespace std;

int main()
{
    int n;

    cout << "Enter number of elements: ";
    cin >> n;

    int arr[100];   // assuming maximum size 100

    cout << "Enter " << n << " elements: ";
    for(int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }

    int num;
    cout << "Enter number to search: ";
    cin >> num;

    bool found = false;

    for(int i = 0; i < n; i++)
    {
        if(arr[i] == num)
        {
            found = true;
            break;
        }
    }

    if(found)
        cout << "Number is present in the array.";
    else
        cout << "Number is not present in the array.";

    return 0;
}

Explanation:

  • First, we input n elements into a one-dimensional array.
  • Then, we input another number to search.
  • Using a loop, we compare each element with the given number.
  • If a match is found, we print that the number is present; otherwise, it is not present.
shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
2024-2025 (July) Official Board Paper
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×