Prime Finding: Sieve of Erastosthenes

I have watched the videos from mycodeschool and implemented it:
code:

//find all prime numbers upto n
//sieve of erastosthenes

#include<bits/stdc++.h>
int main()
{
    int n;
    scanf("%d",&n);
    int primes[n];
    for(int i=0; i<=n; i++)
    {
        primes[i]=1; //make prime to all numbers between 0 and n
    }
    primes[0]=0; //0 not prime
    primes[1]=0; //1 not prime
    for(int i=2; i<=n; i++) //2 to n iterating
    {
        if(primes[i]==1)   //if 2 to n is prime then
        {
            for(int j=2; i*j<=n; j++) //j=2 and i*j<=9 means 2*2<=9  
            {
                primes[i*j]=0;  //primes[i*j=2*2=4] means primes[4]=0;
            }
            printf("%d ",i); //
        }
    }

}

Video:

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *