c++ program to search a number in array

#include<iostream>
using namespace std;
void search_array(int arr[],int num);
int main()
{
int num,arr[100];
cout<<"Enter the size of the array\n";
cin>>num;
cout<<"Enter elements in the array\n";
for(int i=0;i<=num;i++)
{
cin>>arr[i];
}
search_array(arr,num);
}
void search_array(int arr[],int num)
{
int i,number,flag=0;
cout<<"Enter the number to search in the array\n";
cin>>number;
for(i=0;i<=num;i++)
{
if(arr[i]==number)
flag=1;
}
if(flag==1)

cout<<"Found in index "<<i;//i to display index number of array
else
cout<<"Not found";

}
OUT:
Enter the size of the array
2
Enter elements in the array
45
47
12
Enter the number to search in the array
12
Found in index 3


Comments

Popular posts from this blog

cpanel exam CPSP Answers

How to install zimbra collaboration suite 8.8.11 on CentOS 7

awstats installation