Get Update on recent Technology & Programming

Friday, 10 November 2017

C Program to find Prime numbers using sieve Algorithm

Posted by   on Pinterest



C Program to find Prime numbers using sieve Algorithm





#include<stdio.h>

#define MAX 10000

int main(void)

{

	int p,i,n,a[MAX]={0};

		

	printf("Enter n : ");

	scanf("%d",&n);

    

	p=2;

	while(p*p <= n) 

	{

		for(i=2; i*p<=n; i++) 	/*Cross out all multiples of p*/

			a[i*p]=1;

				

		for(i=p+1; i<=n; i++) /*Find next uncrossed*/

			if(a[i]==0)

			{

				p=i;

				break;

			}

	}

	/*Print all uncrossed integers*/

	for(i=2; i<=n; i++)

		if(a[i]==0)

			printf("%d ",i);

	return 0;

}




No comments:
Write comments

Hey, we've just launched a new custom color Blogger template. You'll like it -
Join Our Newsletter