Saturday 9 January 2016

Using SIEVE METHOD Print All Prime Number Till N. (Programming Problem 2.4)


Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.

/**Bismillahir Rahmanir Rahim.**/

#include <stdio.h>
void crossout(int b[], int n);
int main()
{
    int n,i;
    printf("Please, input the limit:\n");
    scanf("%d", &n);
    int a[n];
    for(i=0; i<n; i++)
        a[i]=i+1;
    crossout(a, n);
    return 0;
}
void crossout(int b[], int n)
{
    int k,i;
    int m = sqrt(n);
    for(k=2; k<=m; k++)
    {
        if(b[k-1]!=1)
        {
            for(i= 2*k-1; i<n; i += k)
                b[i]=1;
        }
    }
    for(i=0; i<n; i++)
    {
        if(b[i]!=1)
            printf("%d ", b[i]);
    }
    printf("\n");
}

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.