Skip to content

Instantly share code, notes, and snippets.

@Brli
Created November 9, 2015 14:07
Show Gist options
  • Save Brli/333089c611d3805a2c7f to your computer and use it in GitHub Desktop.
Save Brli/333089c611d3805a2c7f to your computer and use it in GitHub Desktop.
Is prime or not
#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