answersLogoWhite

0


Best Answer

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
This answer is:
User Avatar
More answers
User Avatar

Wiki User

7y ago

#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;

}

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

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;

}

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

#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;

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

#include<stdio.h>

int main (void) {

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

return 0;

}

This answer is:
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
Related questions

Program for print prime all number from 1 to 100 in foxpro?

Prime numbers are numbers that are only divisible by themselves and the number 1. You can write a program to print all prime numbers from 1 to 100 in FoxPro.


Write a program to print first 100 alternative prime numbers?

This would require some computer knowledge. It can make it easier to find out the prime numbers without figuring it out in your head.


What BASIC program can compute and display all prime numbers from 1 to 40?

PRINT 2,3,5,7,11,13,17,19,23,29,31,37


Write a java script program to print first ten odd natural numbers in C?

Q.1 Write a program to print first ten odd natural numbers. Q.2 Write a program to input a number. Print their table. Q.3 Write a function to print a factorial value.


How do you write a program to print numbers to 50 except prime?

First, create a for loop from a,1 to 50. Inside of that create another for loop b,2 to a-1. If a/b=int(a/b) then you know it is not prime


Q2 Write a program to print even numbers between 10 and 50?

You can use int i; for (i = 10; i &lt;= 50; i += 2) {//print i} as a program to print even numbers between 10 and 50.


Print first ten prime numbers in c?

// simple program to generate first ten prime numbers #include&lt;stdio.h&gt; #include&lt;conio.h&gt; void main() { int c,i,j,n; clrscr(); for(i=2;i&lt;30;i++) { c=0; for(j=2;j&lt;i;j++) { if(i%j==0) {c=c+1; } } if(c==0) printf("%d",i); } getch(); }


C program to fine the largest of 10 given number?

first sort the ten numbers in descending order and print the first number. That will be the largest no


Write a C program to find the prime numbers from 1 to 300?

/*the program to print prime no from 1 to 300*/ #include&lt;stdio.h&gt; #include&lt;conio.h&gt; void main() { int i,j; clrscr(); printf("The prime numbers from 1 to 300 are\n"); for(j=2;j&lt;=300;j++) { for(i=2;i&lt;=j/2;i++) if(j%i==0) break; if(i&gt;j/2) { printf("%d ",j); } } }


Write a qbasic program to print the squares and cubes of first 10 natural numbers?

10 CLS 20 FOR n = 1 to 10 30 PRINT n, n^2, n^3 40 NEXT n 50 PRINT: PRINT: PRINT "Touch 'x' to go again, any other key to end." 60 INPUT a$ 70 IF a$ = "X" or a$ = "x" THEN 10 80 END


Write a c program to print prime numbers from 1 to 10000 that has an unit digit which is multiple of 3?

This is a homework question and does not deserve an answer because you will learn nothing other than being lazy.


Write a C Program to print sum of squares of odd numbers?

#include