Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- //Checking primality of a number using TRIAL DIVISION METHOD
- using namespace std;
- int main() {
- unsigned int n;
- cout << "Enter a Positive decimal number: ";
- cin >> n;
- if(n==0||n==1) {
- cout << endl << n << " is neither prime nor composite.\n";
- return 0;
- }
- for(int i=2;i<=sqrt(n);i++) { //Set i to be less than n if sqrt not available and use the below commented method
- if(n%i==0) {
- cout << endl << n << " is not a prime number.\n";
- return 0;
- }
- /* If sqrt function is not supported or cannot be used, take squares of the factors
- and make sure i is set to be less than n and not its square root.
- *****THIS WILL INCREASE THE TIME COMPLEXITY*****
- else if(i*i>n) {
- cout << endl << n << " is a prime number.\n";
- break;
- }*/
- }
- cout << endl << n << " is a prime number.\n";//Discard the statement if using the above commented method
- return 0;
- }
Add Comment
Please, Sign In to add comment