Skip to content

Instantly share code, notes, and snippets.

@bzdgn
Last active October 29, 2015 00:42
Show Gist options
  • Save bzdgn/1ba97a9a79a9e7754962 to your computer and use it in GitHub Desktop.
Save bzdgn/1ba97a9a79a9e7754962 to your computer and use it in GitHub Desktop.
Prime Test Using While
#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)
{
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