answersLogoWhite

0


Best Answer

#include

bool is_prime(unsigned num)

{

// 2 is the first prime and the only even prime.

if (num < 2) return false;

if (num == 2) return true;

if (num % 2 == 0) return false;

// Divide num by every odd number from 3 up

// to and including the square root of num.

// If any division results in no remainder

// then num is not prime.

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

unsigned div = 3;

do {

if (num % div == 0)

return false;

} while ((div += 2) < max);

// If we get this far, num is prime.

return true;

}

int main()

{

std::cout << "Prime numbers in the range 0 to 300:\n" << std::endl;

unsigned num = 0;

do {

if (is_prime (num))

std::cout << '\t' << num << std::endl;

} while( num++ < 300);

std::cout << std::endl;

}

Example Output

Prime numbers in the range 0 to 300:

2

5

7

11

13

17

19

23

29

31

37

41

43

47

53

59

61

67

71

73

79

83

89

97

101

103

107

109

113

127

131

137

139

149

151

157

163

167

173

179

181

191

193

197

199

211

223

227

229

233

239

241

251

257

263

269

271

277

281

283

293

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the program that prints all prime numbers from 0 to 300 in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

A program c plus plus on automorphic numbers or not?

how to write a program that counts automorphic number from 1 to 999


Is there an answer book for the A plus program?

The A Plus Program is an initiative, not a test. So no, there is no answer book.


C plus plus program to find all even numbers between 100 and 150 using for loop?

#includeint main(){int i;for(i=2;i


Write a C plus plus program to find out the square of first 10 numbers?

#include&lt;iostream&gt; int main() { int i=0; while(i++&lt;10) std::cout&lt;&lt;i*i&lt;&lt;std::endl; }


What is the meaning of plus plus in c?

++a (plus plus a) is pre-incrementing operator to aa=10;printf("%d",++a); /* it will print 11 as ++a increment first a by 1 then prints it */printf("%d",a++); /*it will printf 10 as it is post _ increment operator , it prints the value a first then increment it by 1 */

Related questions

How do you write a C plus plus program that will display the first 10 positive prime numbers?

By learning how to program on C+.


What are prime number and composite numbers?

a prime number is a number that you can plus by one


Factor the polynomial. If it is prime type PRIME. x2 plus 5x plus 4?

It is: (x+1)(x+4) none of which are prime numbers


A program c plus plus on automorphic numbers or not?

how to write a program that counts automorphic number from 1 to 999


How many prime numbers between 2000 and 3000?

127 prime numbers in that range. See related link for prime numbers up to 10000. Plus there is a link to download Zip files of prime numbers up to 1 million.


Is 4 plus 6 a prime number of 20?

NO!!!! Other than '2' all prime numbers are odd numbers. 4,6,10 &amp; 20 are all even so they are not prime numbers.


How to Write a c plus plus program to print number from 1 to 500?

#include using std::cout;using std::endl;int main(viod){cout


What is P plus P plus N in prime numbers?

It would always be a natural number.


What two prime numbers equal 98?

19 plus 79


What are to prime numbers to make 30?

They are 23 plus 7 = 30


Where is Prints Plus located in Idaho?

Prints Plus in Idaho is located in Boise City. The address of Prints Plus in Idaho is 1537 N Milwaukee St, Boise City, ID 83704. You can contact Prints Plus in Idaho by phone at (208) 376-4701.


What two numbers does the primorial formula prime number plus or minus 1 produce for prime equals 7 which if either of these numbers is prime?

p7# - 1 = 510509 and p7# + 1 = 510511, neither of which is prime.