#include #include using namespace std; //The n % factor == 0 keeps the inner for loop looping //as long as n is still divisible by the factor. //This won't go on forever, because n keeps getting smaller and smaller. //The n /= factor means n = n / factor //which is the thing that causes n to get smaller and smaller. // int main() { cout << "What positive integer do you want to factor? "; int n {0}; cin >> n; if (!cin) { cerr << "Sorry, that wasn't a number.\n"; return EXIT_FAILURE; } if (n < 2) { cerr << "Sorry, can't factor an integer less than 2.\n"; return EXIT_FAILURE; } for (int factor {2}; factor <= n; ++factor) { for (; n % factor == 0; n /= factor) { cout << factor << "\n"; } } return EXIT_SUCCESS; }