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


Please Help Members By Posting Answers For Below Questions

How does struct work in c?

611


What is merge sort in c?

649


Add Two Numbers Without Using the Addition Operator

357


How many types of sorting are there in c?

618


How would you rename a function in C?

625






what will be maximum number of comparisons when number of elements are given?

1413


Write a code of a general series where the next element is the sum of last k terms.

598


How to draw the flowchart for structure programs?

8764


Are enumerations really portable?

598


How can I list all of the predefined identifiers?

583


Why we not create function inside function.

1753


What is null in c?

602


What is #pragma statements?

595


List the different types of c tokens?

630


Is return a keyword in c?

602