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 #define works?
What are the differences between Structures and Arrays?
write a sorting prgm to sort 50 nos and sum them and also remove all the occurrences of 15 and print it?
The file stdio.h, what does it contain?
What is the difference between exit() and _exit() function?
What does c mean in basketball?
When should you not use a type cast?
How can I write a function that takes a format string and a variable number of arguments?
Does c have class?
Can you please explain the difference between exit() and _exit() function?
If a five digit number is input through the keyboard, write a program to print a new number by adding one to each of its digits.For example if the number that is input is 12391 then the output should be displayed as 23402
What are header files in c?
What is the difference between null pointer and wild pointer?
Write a program on swapping (100, 50)
What is the most efficient way to store flag values?