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
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
