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 you increase the size of a statically allocated array?
What are the advantages of external class?
Is it cc or c in a letter?
What is the difference between null pointer and wild pointer?
What is c language and why we use it?
How can type-insensitive macros be created?
C language questions for civil engineering
What is keyword in c?
Implement bit Array in C.
ATM machine and railway reservation class/object diagram
What does it mean when a pointer is used in an if statement?
Differentiate between the expression “++a” and “a++”?
How do you define structure?
Explain what is a program flowchart and explain how does it help in writing a program?
Why doesnt this code work?