#include using namespace std; /* * This C++ program prints a number's prime factors. * * @creator g.d.thurman * @created 2008.03.09 * @caveats inputs are not checked */ int main(int argc, char* argv[]) { int n, f; for (int i = 1; i < argc; i++) { if ((n = atoi(argv[i])) < 1) continue; // only works with positive integers for (f = 2; f <= n; f++) if (n % f == 0) break; // not a prime number if (f == n) { cout << n << " is prime" << endl; continue; } cout << n << " has the prime factors: "; bool first = true; for (f = 2; f <= n; ) { if (n % f == 0) { if (first) first = false; else cout << "*"; cout << f; n /= f; } else f++; } cout << endl; } return 0; }