Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <cmath>
- #include <vector>
- using namespace std;
- int main()
- {
- int num;
- int count = 0;
- size_t size = 1;
- vector<int> array(size);
- array[0] = 2;
- cout << "Enter a positive integer : ";
- cin >> num;
- while (num < 0)
- {
- cout << "ERROR! Please re-enter a POSITIVE Integer";
- cin >> num;
- }
- for (int s = 2; count < num; s++)
- {
- int rt = int(sqrt(float(s)) + 0.5);
- bool isprime = true;
- for (int n = 1; n < array.size() && array[n] <= rt && isprime; n++)
- {
- if (s % array[n] == 0)
- {
- isprime = false;
- }
- }
- if (isprime)
- {
- cout << s << " is a prime number" << endl;
- array.push_back(s);
- count = count + 1;
- }
- if (count == num)
- {
- cout << "Prime number " << num << " is: " << s << endl;
- }
- }
- cout << "Press enter to finish";
- cin >> num;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement