answersLogoWhite

0

In order to print the first 10 prime numbers you need a function to determine if a given value is prime or not. The following algorithm is the standard method of doing so:

1. Let n be the value.

2. If n < 2 then return false.

3. If n is even then return true if n is 2, otherwise return false.

4. Let divisor = 3.

5. If divisor is greater than the square root of n then return true.

6. If divisor is a factor of value then return false.

7. Let divisor = divisor + 2.

8. Go to step 5.

This algorithm can be efficiently implemented in C++ as follows:

bool is_prime (const unsigned n)

{

if (n<2) return false;

if (!(n&1)) return n==2;

const unsigned m = static_cast<unsigned>(std::sqrt (static_cast<double>(n)));

for (unsigned d=3; d<=m; d+=2)

if (!(n%d)) return false;

return true;

}

With this function defined, we can now go ahead and write the complete program:

#include<iostream>

bool is_prime (const unsigned n) {/*as above*/}

int main()

{

std::cout << "First 10 primes:\n";

unsigned primes = 0;

unsigned num = 0;

while (primes < 10)

{

if (is_prime (num))

{

std::cout << num << std::endl;

++primes;

}

++num;

}

}

User Avatar

Wiki User

10y ago

Still curious? Ask our experts.

Chat with our AI personalities

CoachCoach
Success isn't just about winning—it's about vision, patience, and playing the long game.
Chat with Coach
JudyJudy
Simplicity is my specialty.
Chat with Judy
BeauBeau
You're doing better than you think!
Chat with Beau
More answers

#include<stdio.h>

#include<math.h>

bool is_prime (const unsigned);

unsigned next_prime (unsigned);

int main (void) {

unsigned count=0, num=0;

printf ("The first 100 prime numbers\n");

while (count<100) {

num = next_prime (num);

printf ("%d\t", num);

++count;

}

printf ("\n");

return 0;

}

unsigned next_prime (unsigned num) {

while (!is_prime (++num));

return num;

}

bool is_prime (const unsigned num) {

if (num<2) return false;

if (!(num%2)) return num==2;

const unsigned max = (unsigned) sqrt (num) + 1;

for (unsigned div=3; div<max; div+=2) if (!(num%div)) return false;

return true;

}

User Avatar

Wiki User

8y ago
User Avatar

First, you need a function that can determine if a given number is prime or not:

#include<math.h>

bool is_prime (unsigned num) {

if (num < 2) return false;

if (!(num % 2)) return num == 2; // 2 is the only even prime

unsigned max = (unsigned) sqrt ((double) num) + 1;

for (unsigned div = 3; div < max; div += 2) if (!(num % div)) return false;

return true;

}

Next, a function that will return the next prime after a given number:

unsigned next_prime (unsigned num) {

while (!is_prime (++num));

return num;

}

Finally, a function that counts the number of primes in a given range:

unsigned count_primes (unsigned min, unsigned max) {

unsigned count = is_prime (num) ? 1 : 0;

while ((min = next_prime (min)) <= max) ++count;

return count;

}

To count the number of primes in the range 1 to 1,000,000:

#include<stdio.h>

int main (void) {

unsigned count = count_primes (1, 1000000);

printf ("There are %d primes in the range [1:1,000,000]\n", count);

return 0;

}

User Avatar

Wiki User

8y ago
User Avatar

#include<iostream>

#include<cmath>

bool is_prime (unsigned n) {

if (n<2) return false;

if (!(n%2)) return n==2;

unsigned max {sqrt (n)};

for (unsigned factor {3}; factor<=max; factor+=2)

{

if (!(num%factor)) return false;

}

return true;

}

int main (void)

{

for (unsigned x=100; x<=200; ++x) if (is_prime (x)) std::cout<<x<<std::endl;

}

User Avatar

Wiki User

8y ago
User Avatar

#include<stdio.h>

int main (void) {

printf ("The first 5 prime numbers are 2, 3, 5, 7 and 11.\n");

return 0;

}

User Avatar

Wiki User

9y ago
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a program in c to print the first five prime numbers?
Write your answer...
Submit
Still have questions?
magnify glass
imp