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


Please Help Members By Posting Answers For Below Questions

What is function pointer c?

586


Iam a B.Tech graduate and completed my engineering in 2009, from 2005 to 2009 and after that i had done nothing.Now i want to do job and get into BPO field . Friends give me suggestions as what to say in interview... if they ask me that what would you had done ... these many years without doing job ??????? pls urgent

1428


Which built-in library function can be used to match a patter from the string?

744


What are the c keywords?

750


What is the function of volatile in c language?

664






Suppose we have a table name EMP as below. We want to perform a operation in which, I want to change name ‘SMITH’ from as ‘SMITH JAIN’. Also I want to change the name of the column from ENAME to E_NAME. EMPNO ENAME JOB MGR HIREDATE SAL 7369 SMITH Coder 7902 17-DEC-80 800 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 7521 WARD SALESMAN 7698 22-FEB-81 1250

1505


How do you convert strings to numbers in C?

707


Hello. How to write a C program to check and display president party like if i type in the console "biden" and hit enter the output shoud be : "biden is democrat" and if i type "trump" and hit enter the output shoud be: "trump is republican"

1582


Please send me WIPRO technical question to my mail ID.. its nisha_g28@yahoo.com please its urgent

1648


When reallocating memory if any other pointers point into the same piece of memory do you have to readjust these other pointers or do they get readjusted automatically?

806


How to delete a node from linked list w/o using collectons?

2087


What does %p mean?

590


what is the difference between 123 and 0123 in c?

719


Explain #pragma statements.

604


What is const and volatile in c?

564