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 can I remove the leading spaces from a string?
How can I sort a linked list?
what are bit fields? What is the use of bit fields in a structure declaration?
Why is structure important for a child?
How can I get back to the interactive keyboard if stdin is redirected?
What is the difference between far and near ?
write a program to print data of 5 five students with structures?
How to get string length of given string in c?
Is c++ based on c?
What is an example of structure?
What are volatile variables in c?
How to declare pointer variables?
How can I manipulate individual bits?
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
What is function prototype?