Skip to content

Commit

Permalink
Merge pull request #148 from glazedtorus/mybranch
Browse files Browse the repository at this point in the history
Supplemented Prime_Numbers.cpp file
  • Loading branch information
Twiggecode authored Aug 19, 2021
2 parents 742981c + cd5d04b commit 3ad3adb
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Prime Numbers/Prime_Numbers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,29 @@ Is_Prime(7) --> will return 'true' value
// Boolean function that retutn TRUE if the input is Prime, else return FALSE
bool Is_Prime(int n) {

if (n <= 1 || n % 2 == 0 && n!=2) // If less than 2, or even number, and not 2 --> not prime
if (n <= 1 || (n % 2 == 0 && n!=2)) // If less than 2, or even number, and not 2 --> not prime
return false;

if (n == 2) // 2 is prime
return true;

for (int i = n / 2 + 1; i > 1; i = i - 2) // Any valid divisor, if exist, must be odd and <= n\2
if (n % i == 0)
if (!(n % i))
return false;

return true;
}

int main() {
int n;

std::cout<<"Enter an integer greater than 1: ";
std::cin>>n;

Is_Prime(n);
if(Is_Prime(n)==true){
std::cout<<"True";
}else{
std::cout<<"False";
}
}

0 comments on commit 3ad3adb

Please sign in to comment.