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

What else can I help you with?

Related Questions

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


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

Oh, what a lovely request! In FoxPro, you can create a program to print all prime numbers from 1 to 100 by using a loop to check each number for divisibility only by 1 and itself. If it meets this criteria, you can print it out on the screen. Remember, every number is unique and special, just like a happy little tree in a vast forest.


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 sum of squares of odd numbers?

#include


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.