#include #include #include using namespace std ; void printPrimeFactors(int num) { static int divisor = 2; // 2 is the first prime number if ( num == 1 ) //if num = 1 we finished { divisor = 2; //restore divisor, so it'll be ready for the next run cout << endl ; return; } else if ( num % divisor == 0 ) //if num divided by divisor { cout << divisor << " "; //print divisor printPrimeFactors( num / divisor ); //call the function with num/divisor } else //if num not divided by divisor { divisor++; //increase divisor printPrimeFactors( num ); } } void printPrimeFactorsIter(int num) { int divisor = 2; // 2 is the first prime number while ( num > 1 ) { if ( num % divisor == 0 ) { cout << divisor << " "; //print divisor num = num / divisor ; } else divisor++ ; } //while cout << endl ; } int main() { int x1 ; cout << "Input number:" ; cin >> x1 ; printPrimeFactors( x1 ) ; printPrimeFactorsIter( x1 ) ; cout << endl ; }