Recursive function to get divisors - C++ Function

C++ examples for Function:Recursive Function

Description

Recursive function to get divisors

Demo Code

#include <iostream>
#include <cmath>
using namespace std;
void get_divisors(int n);
int main()/*from w  w  w .  j a  v a  2s . c o  m*/
{
   int n = 0;
   cout << "Enter a number and press ENTER: ";
   cin >> n;
   get_divisors(n);
   cout << endl;
   return 0;
}
void get_divisors(int n) {
   double sqrt_of_n = sqrt(n);
   for (int i = 2; i <= sqrt_of_n; ++i) {
      if (n % i == 0) {   // If i divides n evenly,
         cout << i << ", ";    //   Print i,
      get_divisors(n / i);  //   Factor n/i,
      return;               //   and exit.
   }
}
// If no divisor is found, then n is prime; Print n and make no further calls.
cout << n;
}

Result


Related Tutorials