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 the role of this pointer?
What is c programing language?
What is ## preprocessor operator in c?
Who developed c language and when?
How will you delete a node in DLL?
How can I implement sets or arrays of bits?
What is an expression?
Can we change the value of constant variable in c?
How do you list files in a directory?
Is swift based on c?
What standard functions are available to manipulate strings?
What is difference between class and structure?
Write a program that accept anumber in words
What are examples of structures?
What are dangling pointers? How are dangling pointers different from memory leaks?