Skip to content

Instantly share code, notes, and snippets.

@iahmadgad
Created November 21, 2023 18:36
Show Gist options
  • Save iahmadgad/d69c64cc6b5404387381950b7440c18e to your computer and use it in GitHub Desktop.
Save iahmadgad/d69c64cc6b5404387381950b7440c18e to your computer and use it in GitHub Desktop.
C++ prime number checker
using namespace std;
int main()
{
long x;
cin >> x;
if(isPrime(x)) cout << "PRIME" << endl;
else cout << "NOT PRIME" << endl;
}
bool isPrime(long n)
{
    if(n == 0 || n == 1) return false;
    for(long i = 2; i * i <= n; i++)
    {
        if(n % i == 0) return false;
    }
    return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment