Advertisement
Guest User

C++ Prime Testing Program

a guest
Jan 24th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 KB | None | 0 0
  1. #include <iostream>    // includes common inupt output functions
  2. #include <time.h>       //for the time / duration
  3.  
  4. using namespace std;    // allows for the use of named functions
  5.  
  6.  
  7. int main()  // main program
  8. {
  9.    
  10.     int n;   //our number
  11.     cout << "Welcome to the Primality Testing Program\n";
  12.     cout << "Please enter a number to test if it is a prime.\n";   //welcome text
  13.     cout << "Enter Number: ";                   //prompts user for variable 'n'
  14.     cin >> n;     //gathers input into variable n
  15.     cout << "___________________________________________________________\n";
  16.  
  17.     bool is_prime = true;  //default assumption is that the number is a prime, until tested
  18.  
  19.     //
  20.     int i;   //loop iteration
  21.     int j;  //result of modulus operation to determine if result is 0 -
  22.             //if 0 then the "i" tested can divide n
  23.     int componentArray[25]; //container for found components
  24.     int primeCounter = 0;  //counter for the component array
  25.  
  26.     i = 2;  //starting counter at 2 because 1 and 2 are both prime
  27.     clock_t start;
  28.     double duration;
  29.     start = clock();
  30.  
  31.     while (i < n)   //tests numbers below n
  32.     {
  33.         int j = n%i;  //sets the result of the modulus operation to j
  34.         duration = clock();  //function to return number of ticks
  35.  
  36.         if (j == 0)    //if there is no remainder
  37.         {
  38.             primeCounter++; //bump counter up , making room for component in array
  39.  
  40.             is_prime = false;   //set prime to false -because the mod operation provided a zero
  41.             cout << "########################################################\n";
  42.             cout << "BOOOM!\nYour number is divisible by " << i;
  43.             cout << "\n########################################################\n";
  44.             //this displays a message when the cycle hits a prime
  45.             componentArray[primeCounter] = i; //fills current position, in order, with next found multiple(component)
  46.  
  47.             cout << endl;
  48.  
  49.         }  
  50.  
  51.    
  52.         cout << n << "%" << i << "=" << j << endl;  //displays continuous lines, showing which number is being tested, and the result
  53.         i++;  //steps the loop forward
  54.     }
  55.  
  56.     if (is_prime)    //if the number is prime (this is a bool variable/ flagged false if the mod operation provided a zero)
  57.     {
  58.         cout << endl;
  59.         cout << endl;
  60.         cout << "___________________________________________________________\n";
  61.         cout << "Your number is a prime" << endl;  //display text showing the number is a prime
  62.     }
  63.     else
  64.     {
  65.         cout << endl;
  66.         cout << endl;
  67.         cout << "___________________________________________________________\n";
  68.         cout << "Your number is not a prime" << endl;  //display text showing the number is not a prime
  69.         cout << "Your number is composed of: 1 x ";
  70.         for (i = 1; i < primeCounter+1; i++)
  71.             /*cout << i << endl;*/
  72.             cout << componentArray[i] << " x " ;   //displays each position of the component array
  73.         cout << " " << n;  
  74.  
  75.     }
  76.     cout << endl;
  77.     cout << endl;
  78.     cout << "___________________________________________________________\n";
  79.     duration = (clock() - start) / (double)CLOCKS_PER_SEC;
  80.     cout << "It took " << duration << " seconds to define the components of " << n;
  81.  
  82.  
  83.     cin.get();
  84.     cin.get();  //keeps the window open
  85.     return 0;  //returns 0 to indicate no error - part of the programming language
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement