Skip to content

Instantly share code, notes, and snippets.

@bzdgn
Created October 29, 2015 00:41
Show Gist options
  • Save bzdgn/767487ca1310ad826ed6 to your computer and use it in GitHub Desktop.
Save bzdgn/767487ca1310ad826ed6 to your computer and use it in GitHub Desktop.
Prime Test Using For
#include <iostream>
using namespace std;
int main()
{
int x;
cout << "Enter a number" << endl;
cin >> x;
bool prime = true;
//int i = 2;
//while (i <= x / i)
for (int i = 2; i <= x / i; i = i + 1)
{
int factor = x / i;
if (factor*i == x)
{
cout << "factor found: " << factor << endl;
prime = false;
break;
}
//i = i + 1;
}
cout << x << " is ";
if (prime)
cout << "prime" << endl;
else
cout << "not prime" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment