Advertisement
Guest User

Malu test

a guest
May 30th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <cmath>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. int num;
  11. int count = 0;
  12. size_t size = 1;
  13. vector<int> array(size);
  14. array[0] = 2;
  15.  
  16. cout << "Enter a positive integer : ";
  17. cin >> num;
  18.  
  19. while (num < 0)
  20. {
  21. cout << "ERROR! Please re-enter a POSITIVE Integer";
  22. cin >> num;
  23. }
  24.  
  25. for (int s = 2; count < num; s++)
  26. {
  27. int rt = int(sqrt(float(s)) + 0.5);
  28.  
  29. bool isprime = true;
  30.  
  31. for (int n = 1; n < array.size() && array[n] <= rt && isprime; n++)
  32. {
  33. if (s % array[n] == 0)
  34. {
  35. isprime = false;
  36. }
  37. }
  38. if (isprime)
  39. {
  40. cout << s << " is a prime number" << endl;
  41. array.push_back(s);
  42. count = count + 1;
  43. }
  44. if (count == num)
  45. {
  46. cout << "Prime number " << num << " is: " << s << endl;
  47. }
  48. }
  49.  
  50. cout << "Press enter to finish";
  51. cin >> num;
  52.  
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement