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
How can I find the modification date of a file?
What is the equivalent code of the following statement in WHILE LOOP format?
Why do we use int main instead of void main in c?
What does node * mean?
Can you pass an entire structure to functions?
What are the types of macro formats?
Should I learn c before c++?
What does 2n 4c mean?
What is the use of pointers in C?
What is the best organizational structure?
How do we declare variables in c?
Describe wild pointers in c?
What are 'near' and 'far' pointers?
What is formal argument?
What is return type in c?