Last active
October 29, 2015 00:42
-
-
Save bzdgn/1ba97a9a79a9e7754962 to your computer and use it in GitHub Desktop.
Prime Test Using While
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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