Created
October 29, 2015 00:41
-
-
Save bzdgn/767487ca1310ad826ed6 to your computer and use it in GitHub Desktop.
Prime Test Using For
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) | |
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