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 are the standard predefined macros?
Explain about the constants which help in debugging?
Explain union.
How many loops are there in c?
Explain what are reserved words?
How to explain the final year project as a fresher please answer with sample project
What is the method to save data in stack data structure type?
What is structure in c definition?
What are the two types of structure?
What is printf () in c?
Why is c platform dependent?
Can you think of a logic behind the game minesweeper.
Who developed c language and when?
What is substring in c?
How do c compilers work?