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 sizeof array?
Which header file is used for clrscr?
FILE PROGRAMMING
What is the scope of static variable in c?
What is ## preprocessor operator in c?
Explain why c is faster than c++?
What are global variables?
Explain how can I remove the trailing spaces from a string?
What are the valid places to have keyword “break”?
What does 1f stand for?
Can we assign integer value to char in c?
How can you draw circles in C?
how many types of operators are include in c language a) 4 b) 6 c) 8 d) 12
which is conditional construct a) if statement b) switch statement c) while/for d) goto
What is the difference between exit() and _exit() function?