Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream> // includes common inupt output functions
- #include <time.h> //for the time / duration
- using namespace std; // allows for the use of named functions
- int main() // main program
- {
- int n; //our number
- cout << "Welcome to the Primality Testing Program\n";
- cout << "Please enter a number to test if it is a prime.\n"; //welcome text
- cout << "Enter Number: "; //prompts user for variable 'n'
- cin >> n; //gathers input into variable n
- cout << "___________________________________________________________\n";
- bool is_prime = true; //default assumption is that the number is a prime, until tested
- //
- int i; //loop iteration
- int j; //result of modulus operation to determine if result is 0 -
- //if 0 then the "i" tested can divide n
- int componentArray[25]; //container for found components
- int primeCounter = 0; //counter for the component array
- i = 2; //starting counter at 2 because 1 and 2 are both prime
- clock_t start;
- double duration;
- start = clock();
- while (i < n) //tests numbers below n
- {
- int j = n%i; //sets the result of the modulus operation to j
- duration = clock(); //function to return number of ticks
- if (j == 0) //if there is no remainder
- {
- primeCounter++; //bump counter up , making room for component in array
- is_prime = false; //set prime to false -because the mod operation provided a zero
- cout << "########################################################\n";
- cout << "BOOOM!\nYour number is divisible by " << i;
- cout << "\n########################################################\n";
- //this displays a message when the cycle hits a prime
- componentArray[primeCounter] = i; //fills current position, in order, with next found multiple(component)
- cout << endl;
- }
- cout << n << "%" << i << "=" << j << endl; //displays continuous lines, showing which number is being tested, and the result
- i++; //steps the loop forward
- }
- if (is_prime) //if the number is prime (this is a bool variable/ flagged false if the mod operation provided a zero)
- {
- cout << endl;
- cout << endl;
- cout << "___________________________________________________________\n";
- cout << "Your number is a prime" << endl; //display text showing the number is a prime
- }
- else
- {
- cout << endl;
- cout << endl;
- cout << "___________________________________________________________\n";
- cout << "Your number is not a prime" << endl; //display text showing the number is not a prime
- cout << "Your number is composed of: 1 x ";
- for (i = 1; i < primeCounter+1; i++)
- /*cout << i << endl;*/
- cout << componentArray[i] << " x " ; //displays each position of the component array
- cout << " " << n;
- }
- cout << endl;
- cout << endl;
- cout << "___________________________________________________________\n";
- duration = (clock() - start) / (double)CLOCKS_PER_SEC;
- cout << "It took " << duration << " seconds to define the components of " << n;
- cin.get();
- cin.get(); //keeps the window open
- return 0; //returns 0 to indicate no error - part of the programming language
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement