Prime numbers for a given range

Take as input a Number N and Print all the prime numbers from 2 to N

#include
using namespace std;
void prime(int *a)
{
for(int i=3;i<=1000000;i+=2)
a[i]=1;

for(long long i=3;i<=1000000;i+=2)
{
if(a[i]==1)
{
for(long long j=i*i;j<=1000000;j+=i)
a[j]=0;
}
}
a[0]=a[1]=0;
a[2]=1;
}
int main()
{

int a[1000005]={0};
prime(a);

int n;
cin>>n;
for(int i=2;i<=n;i++)
{
if(a[i]==1)
cout<<i<<", ";

}
cout<<"\b\b\b"<<endl ;

return 0;

}

why am i not getting correct output with all the test cases.

Make the array global.

done still not working

Please share the changed code by running it on the coding blocks ide, ide.codingblocks.com

there is an error due to } but thats not the real issue find the real one.

I told you make the array global, it’s still not global

still but how to remove the last ,(comma)?

I don’t understand, wait I’ll correct the code myself.

Here is your corrected code https://ide.codingblocks.com/s/319248

thanks
for the help keshav


whats wrong in this code to reverse the array

Change your loop to

for(int i=0;i<a/2+(a%2);i++)

{

    swap(ar[i],ar[a-i-1]);

}

still not getting the correct answer

only

can you provide me with the link of the question

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.