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
What is static volatile in c?
In which language linux is written?
Is it better to bitshift a value than to multiply by 2?
Do you know the use of 'auto' keyword?
What is c mainly used for?
What are header files? What are their uses?
.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; }
What do you mean by a local block?
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
What oops means?
What is the scope of static variable in c?
Tell me about low level programming languages.
How can I invoke another program or command and trap its output?
write a progrmm in c language take user interface generate table using for loop?
Which function in C can be used to append a string to another string?