Hello,
I just tried to make the same code taught to us in the video, but the case when a number isn’t present doesn’t get executed, i.e, when i==n, I want to print that the number is not present in the given array. Please help me by telling me where I’m wrong. Here is my code-
#include
using namespace std;
int main()
{
int a[50],key,n,i;
cout<<"Enter size of the array\n";
cin>>n;
cout<<"Enter elements of the array\n";
for(int j=0;j<n;j++)
{
cin>>a[j];
}
cout<<"Enter element to be searched\n";
cin>>key;
for(i=0;i<=n-1;i++)
{
if(a[i]==key){
cout<<"Element found at position number "<<i;
}
}
if(i==n){
cout<<"Element does not exist";
}
return 0;
}