Created
November 9, 2015 14:07
-
-
Save Brli/333089c611d3805a2c7f to your computer and use it in GitHub Desktop.
Is prime or not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cmath> | |
bool isPrime(long n) { | |
long m = (long)sqrt(n); | |
if (m != 2) { | |
while (m > 2){ | |
if(n % m == 0) { | |
return false; | |
} | |
else { | |
--m; | |
} | |
} | |
} | |
else if (n % m == 0) | |
return false; | |
else | |
return true; | |
} | |
int main(int argc, char **argv) { | |
long num; | |
std::cout << "Please enter a number: " << std::endl; | |
std::cin >> num; | |
if (isPrime(num) == true) | |
std::cout << "The number is prime." << std::endl; | |
else | |
std::cout << "The number is divisable!" << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment