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
Was 2000 a leap year?
How can I call a function with an argument list built up at run time?
Explain what is a 'locale'?
What are the salient features of c languages?
What is indirection? How many levels of pointers can you have?
Explain output of printf("Hello World"-'A'+'B'); ?
Do character constants represent numerical values?
Explain the array representation of a binary tree in C.
Why is c called a structured programming language?
How to define structures? ·
How can I direct output to the printer?
What are nested functions in c?
Is c programming hard?
What is formal argument?
What is difference between main and void main?