Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- using namespace std;
- // This program displays all the prime numbers up to a user input
- // Remove multiples of an input from the array
- void removeMultiples(int input, int limit, vector<bool>& primeList) {
- for (int i = 2; i < (limit / input); ++i) {
- primeList[i * input] = false;
- }
- }
- int main() {
- cout << "Lists prime numbers up to chosen limit" << endl << endl; // Two endl to leave a space before listing the numbers
- cout << "Please type in a number to use as a limit, then press 'Enter'" << endl;
- int limit;
- cin >> limit;
- cout << endl; // Visually split input number from output list
- vector<bool> primeList(limit);
- primeList.flip();
- for (int i = 2; i < limit; i++) {
- removeMultiples(i, limit, primeList);
- }
- for (int i = 2; i < limit; i++) {
- if (primeList[i] == true)
- cout << i << endl;
- }
- cout << endl << "Press 'Enter' to exit program" << endl;
- cin.ignore(100, '\n'); // ignore any characters in the input buffer until we find an enter character
- cin.get(); // get one more char from the user
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement