Wrong answer for some test cases

#include
using namespace std;
long long int primvisit(long long int a,long long int b)
{
long long int x=0;
for(long long int i=a;i<=b;i++)
{int flag=0;
if(i%2==0&&i>2)
flag=1;
else
{
for(long long int j=2;j<=i/2;j++)
{if(i%j==0)
flag=1;
}
}
if(flag==0&&i!=1)
{
cout<<i<<" ";
x++;}
}

return x;
}

int main()
{int n;
cin>>n;
int w;
w=n;
if(n<=1000&&n>0)
{long long int *printn=new long long int[n];
long long int a,b;
while(n!=0)
{
cin>>a>>b;
if((a<=1000000&&a>0)&&(b<=1000000&&b>0)&&a<=b)
{
printn[n-1]=primvisit(a,b);
}
else
cout<<“enter a b correctly”;
n–;
}
if((a<=1000000&&a>0)&&(b<=1000000&&b>0)&&a<=b)
{
for(int i=w-1;i>=0;i–)
{cout<<printn[i]<<endl;
}
}
delete [] printn;
}
else
cout<<“enter n properly”;

}
Respected sir
What is the error in this code as this code is not satisfying all the test cases? Is there any way to optimize this code for prime visit?

please save your code on the online ide

In online ide, for a=1 and b=1000000 it is showing the error of time limit exceed. how to resolve it?