prime number generator

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 4 Exercise 11
Using std_lib_facilities.h by Bjarne Stroustrup.

[code language=”cpp”]
// Philipp Siedler
// Bjarne Stroustrup’s PP
// Chapter 4 Exercise 11

#include "std_lib_facilities.h"

int main() {

int max = 100;
vector<int> seriesNum;
vector<int> primeNum;

for (int i = 2; i < max; i++) {
int count = 0;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
count++;
}
}
if (count == 0){
primeNum.push_back(i);
}
}

for (int i = 0; i < primeNum.size(); i++) {
cout << primeNum[i] << "\n";
}

keep_window_open(".");
}
[/code]

Output:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

Leave a Reply