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 can I remove the leading spaces from a string?

858


How can I sort a linked list?

826


what are bit fields? What is the use of bit fields in a structure declaration?

1797


Why is structure important for a child?

855


How can I get back to the interactive keyboard if stdin is redirected?

911


What is the difference between far and near ?

952


write a program to print data of 5 five students with structures?

1855


How to get string length of given string in c?

829


Is c++ based on c?

866


What is an example of structure?

813


What are volatile variables in c?

723


How to declare pointer variables?

930


How can I manipulate individual bits?

821


Hai,I have done with my bachelor of commerce and planing to ms,please suggest me how to convince vo for shifting from commerce to computers. Visa on 8 DEC 2014  Npu university

1839


What is function prototype?

818