write the program for prime numbers?
Answer Posted / dharanya
Answer
# 10
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 ? | 0 Yes | 2 No |
Post New Answer View All Answers
How does struct work in c?
What is merge sort in c?
Add Two Numbers Without Using the Addition Operator
How many types of sorting are there in c?
How would you rename a function in C?
what will be maximum number of comparisons when number of elements are given?
Write a code of a general series where the next element is the sum of last k terms.
How to draw the flowchart for structure programs?
Are enumerations really portable?
How can I list all of the predefined identifiers?
Why we not create function inside function.
What is null in c?
What is #pragma statements?
List the different types of c tokens?
Is return a keyword in c?