I recently learned Sieve of Eratosthenes to find prime numbers. After knowing the method i wrote this code for it.
Would it be a valid C++ code for Sieve of Eratosthenes?
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
bool array[n];
for(int i=2;i<=n;i++)
{
array[i]=true;
}
for(int i=2;i<=n/2;i++)
{
for(int j=i+1;j<=n;j++)
{
if(array[j]==true)
{
if(j%i==0)
array[j]=false;
}
}
}
for(int k=2;k<=n;k++)
{
if(array[k]==true)
cout<<k<<" ";
}
}
Thanks for help!