Skip to content

Instantly share code, notes, and snippets.

@lablnet
Created March 12, 2020 14:29
Show Gist options
  • Select an option

  • Save lablnet/ef93c82abd7f1f6d8c80be0f0cd960d7 to your computer and use it in GitHub Desktop.

Select an option

Save lablnet/ef93c82abd7f1f6d8c80be0f0cd960d7 to your computer and use it in GitHub Desktop.
C++ prime find prime number using exhaustive enumeration
#include <iostream>
using namespace std;
namespace lablnet {
bool is_prime(int n)
{
bool isPrime = true;
for (int i = n - 1; i >= 2; i--)
{
if (n % i == 0)
{
isPrime = false;
break;
}
}
return isPrime;
}
}
int main()
{
bool isPrime = true;
int n;
cout << "Enter a positive integer \t";
cin >> n;
if (lablnet::is_prime(n))
cout << "This is a prime number";
else
cout << "This is not a prime number";
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment