Guest User

Untitled

a guest
Feb 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1.  
  2.  
  3.  
  4. // include necessary libraries
  5. // those are required for some types to work.
  6. #include <iostream>
  7.  
  8.  
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13.     //declaring required variables.
  14.     int num = 0;
  15.     int c_prime = 0;
  16.  
  17.  
  18.     //prompting user for inputs.
  19.     cout << "Please enter an integer greater than 1 : " << endl;
  20.     cin >> num;
  21.     //extra line for spacing.
  22.     cout << endl;
  23.                             //if the user happens to input a value less than 1, let him/her knows.
  24.                             //because this program was meant to find prime from 1.
  25.                             if(num<1){
  26.                                 cout << "You will have to enter a number greater than 1, sorry.";
  27.  
  28.                             }
  29.                                 //goes only if the user input, num is greater than 1.
  30.                                 while(num >1){
  31.  
  32.                                     //starts a for loop where d_num is assigned as num decreasing by 1 and as a decrementing value.
  33.                                     for(int d_num = (num-1); d_num>1; d_num--){
  34.  
  35.                                         //assign the value of num to d_num.
  36.                                         //where num will keep decreasing and
  37.                                         //do a series of check to see if it's prime or not.
  38.                                         num = d_num;
  39.  
  40.                                     //2 is a prime exception where it is the only even number.
  41.                                     //we also know it is always prime.
  42.                                     //this will output num if and when it becomes 2 before or after doing the loop
  43.                                     //at least once.
  44.                                     if(num ==2){
  45.                                         cout << num << " is a prime number." << endl;
  46.                                     }
  47.  
  48.                                     //c_prime is the remainder after dividing num by 2.
  49.                                     //to check if it is divisible by 2.
  50.                                     //there are not any prime numbers that is even except 2.
  51.                                     c_prime = num%2;
  52.  
  53.                                     //this if loop runs if there is the remainder is not 0,
  54.                                     //that means that this number,num might be a prime number.
  55.                                     if(c_prime != 0){
  56.  
  57.                                         for(int d_num = (num-1); d_num >2; d_num--){
  58.  
  59.                                         //assign c_prime the value of the remainder of num divided by d_num, which is
  60.                                         //a decreasing set of numbers starting from the value of num.
  61.                                         c_prime = num%d_num;
  62.  
  63.                                         //check if the number, num has no remainder.
  64.                                         //means that number is absolutely not a prime number.
  65.                                         //and it will break the loop, starting program from top.
  66.                                          if(c_prime == 0){
  67.                                             break;
  68.                                          }
  69.                                         }
  70.  
  71.                                         //final check, checking if the number, num
  72.                                         //has a remainder where c_prime will be greater than 0.
  73.                                         //this means the num here is prime.
  74.                                         if(c_prime !=0){
  75.  
  76.                                         //print out the prime number.
  77.                                         cout << num << " is a prime number." << endl;
  78.  
  79.                                         //another case, if the value of c_prime at this point is 0,
  80.                                         //it is absolutely not a prime number.
  81.                                         //and this break the loop.
  82.                                         }else if(c_prime == 0){
  83.                                                  break;
  84.                                                  }
  85.                                                 //break the loop so that it starts checking other numbers.
  86.                                                  break;
  87.  
  88.                                     }
  89.                                 }
  90.                             }
  91.  
  92.     return 0;
  93. }
Add Comment
Please, Sign In to add comment