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

What is static volatile in c?

758


In which language linux is written?

931


Is it better to bitshift a value than to multiply by 2?

868


Do you know the use of 'auto' keyword?

879


What is c mainly used for?

824


What are header files? What are their uses?

849


.find the output of the following program? char*myfunc(char*ptr) { ptr +=3; return (ptr); } int main() { char*x,*y; x="HELLO"; y=myfunc(x); printf("y = %s ",y); return 0; }

2317


What do you mean by a local block?

839


the statement while(i) puts the entire logic in loop. this loop is called a) indefinite loop b) definite loop c) loop syntax wrong d) none of the above

805


What oops means?

753


What is the scope of static variable in c?

728


Tell me about low level programming languages.

877


How can I invoke another program or command and trap its output?

866


write a progrmm in c language take user interface generate table using for loop?

1806


Which function in C can be used to append a string to another string?

913