help me with this code. (abacus 14 help ram)

#include
#include<math.h>
using namespace std;
int main()
{
int t,i,m,n;
long long a,b,c,j,k,h[100];
cin>>t;
for(i=0;i<t;i++)
{
cin>>a>>b;
for(j=b;j>=a;jā€“)
{
m=0;
n=0;
for(k=2;k<sqrt(j);k++)
if(j%k==0)
{
m=1;
break;
}
if(m==1)
{
n=1;
h[i]=j;
break;
}

		}
		if(n==0)
		h[i]=-1;
	}
	for(i=0;i<t;i++)
	cout<<h[i]<<endl;
}

this code gives wrong ans. can you please find the error

Your code has some errors. First is that you are checking j%k==0 and then you are assigning m=1 which is followed by a break statement. So the condition of m==1 is never taken.

1 Like

well the code is right in this aspect because the if statement is attached to its preceding for statement. so it ll come out of that for loop nd if with condition m==1 is attached to the other for loop.
actually this code is giving correct output but it is not getting submitted.