Compiler not executing the case where a number is not present in the array

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;

}

@karantaneja Your code is running fine.
Which compiler are your use, use https://ide.codingblocks.com/

I’m using Sublime text. I’ll try the CB ide.

@karantaneja try and let me know if you are facing any difficulty.
Also mark this doubt as resolved after your are able to do it.

Got it, it’s running fine now, thank you.

1 Like