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
What do you mean by scope of a variable in c?
Why c is called object oriented language?
Explain is it better to bitshift a value than to multiply by 2?
What is meant by recursion?
What is nested structure?
How can a number be converted to a string?
Tell me when would you use a pointer to a function?
the process of defining something in terms of itself is called (or) in C it is possible for the functions to call themselves. A function called a) nested function b) void function c) recursive function d) indifinite function
write a program to copy the string using switch case?
In c programming typeing to occupy the variables in memory space. if not useing the variable the memory space is wasted.ok, how to avoid the situation..? (the variable is used & notused)
What is break statement?
What is #line used for?
a parameter passed between a calling program and a called program a) variable b) constant c) argument d) all of the above
explain what are pointers?
What is a null pointer in c?