Prime Number: Trial division | Prime check upto N in C

code:

#include<bits/stdc++.h>

int main()
{
    int i,j,n,flag=0;
    printf("enter number: ");
    scanf("%d",&n);
    for(i=2;i<=n;i++) //first iterate upto n
    {
        flag=1;
        for(j=2;j<=sqrt(i);j++) //trial division is using here //(i/2) can also use instead of sqrt(i)
        {
            if(i%j==0)
            {
                flag=0;
                break;
            }
        }
        if(flag==1)
        {
            printf("%d \t",i);
        }
    }


return 0;

}

Help can be find:
http://www.studystreet.com/c-program-print-prime-numbers/
http://www.codingalpha.com/prime-number-c-program/
https://en.wikipedia.org/wiki/Primality_test

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 *