write the program for prime numbers?

Answer Posted / china

Find all primes not larger than N.
I think it is the most efficient algorithm to find all
primes no larger than N.

int main(void)
{
int i,j, N;
int *pPrimes;
int nPrimes, is_prime;
printf("Input N:");
scanf("%d", &N);

pPrimes = new int [N/2];
nPrimes = 0;
for(i = 2; i<=N; i++)
{
is_prime = 1;
for(j=0;j<nPrimes; j++)
if (i%pPrimes[j] == 0)
{
is_prime = 0; break;
}
if (is_prime)
{
pPrimes[nPrimes++] = i;
}
}

printf("%d primes found less than %d:\n", nPrimes, N);
for (i=0; i< nPrimes; i++)
printf("%d ", pPrimes[i]);

}

Is This Answer Correct ?    80 Yes 77 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Was 2000 a leap year?

635


How can I call a function with an argument list built up at run time?

646


Explain what is a 'locale'?

587


What are the salient features of c languages?

627


What is indirection? How many levels of pointers can you have?

665






Explain output of printf("Hello World"-'A'+'B'); ?

981


Do character constants represent numerical values?

848


Explain the array representation of a binary tree in C.

732


Why is c called a structured programming language?

686


‎How to define structures? · ‎

636


How can I direct output to the printer?

820


What are nested functions in c?

569


Is c programming hard?

580


What is formal argument?

654


What is difference between main and void main?

631