Last active
November 8, 2015 05:04
-
-
Save Brli/c69f54f7754ad2a1995e to your computer and use it in GitHub Desktop.
test if a int is prime
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(int n) { | |
int m = (int)std::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) { | |
int n; | |
std::cout << "Please enter a number: " << std::endl; | |
std::cin >> n; | |
if (isPrime(n) == 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