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 the c source code for the below output? 5555555555 4444 4444 333 333 22 22 1 1 22 22 333 333 4444 4444 5555555555

2814


What is static and auto variables in c?

819


How do you convert a decimal number to its hexa-decimal equivalent.Give a C code to do the same

904


int main() { Int n=20,i; For(i=0;i<=n;i--) { Printf(“-“); Return 0;

1413


main() { inta=10,b=20; a>=5?b=100:b=200; printf("%d ",b); }

1195


What is meant by type specifiers?

913


Can true be a variable name in c?

782


In the DOS enveronment, normal RAM that resides beyond the 1mb mark. a) expanded memory b) swapped memory c) Extended memory d) none

1007


i have to apply for the rbi for the post of officers. i need to know abt the entrance questions whether it may be aps or techinical....

1749


With the help of using classes, write a program to add two numbers.

864


How to delete a node from linked list w/o using collectons?

2416


Why can arithmetic operations not be performed on void pointers?

838


What are the types of i/o functions?

1006


how to capitalise first letter of each word in a given string?

1700


What are the advantages of using Unions?

876