Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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 sizeof array?

1002


Which header file is used for clrscr?

999


FILE PROGRAMMING

2179


What is the scope of static variable in c?

944


What is ## preprocessor operator in c?

1025


Explain why c is faster than c++?

1005


What are global variables?

1117


Explain how can I remove the trailing spaces from a string?

991


What are the valid places to have keyword “break”?

1039


What does 1f stand for?

1089


Can we assign integer value to char in c?

1158


How can you draw circles in C?

1099


how many types of operators are include in c language a) 4 b) 6 c) 8 d) 12

1017


which is conditional construct a) if statement b) switch statement c) while/for d) goto

1166


What is the difference between exit() and _exit() function?

992