Write a C Program for Prime numbers using sieve

Admin

 

Write a C Program for Prime numbers using sieve

#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;
}
Output
Enter n : 13
2 3 5 7 11 13 

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.