Advertisement
SageTheWizard

Prime Number Finder

Nov 8th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int counter = 0;
  9.     int j = 3;
  10.     int input;
  11.     bool registered;
  12.    
  13.     ofstream primeOutput;
  14.     primeOutput.open("output.txt");
  15.    
  16.     cout << "Enter A Number and I will find all the prime number from 1 - X: ";
  17.     cin >> input;
  18.     if (input >= 2)
  19.     {
  20.         primeOutput << 2 << "\n";
  21.         counter++;
  22.     }
  23.  
  24.  
  25.     for (int i = 1; i <= (input); i = i + 2)
  26.     {
  27.         registered = false;
  28.         for (int j = 1; j < 10; j++)
  29.         {
  30.             if ((i % j == 0) && (i != j) && (j != 1) && !registered || (i == 1))
  31.             {
  32.                 registered = true;
  33.             }
  34.             else if (!registered && (j == 9))
  35.             {
  36.                 primeOutput << i << "\n";
  37.                 counter++;
  38.             }
  39.    
  40.         }
  41.     }
  42.     cout << "Solutions can be found in this file's directory named \"output.txt\"" << endl;
  43.  
  44.     system("pause");
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement