Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. void get_divisors(int n);
  6.  
  7. int main() {
  8.  
  9. int n = 0;
  10.  
  11. cout << "Enter a number and press ENTER: ";
  12.  
  13. cin >> n;
  14.  
  15. get_divisors(n);
  16.  
  17. cout << endl;
  18.  
  19. return 0;
  20.  
  21. }// end main
  22.  
  23. // Get divisors function
  24.  
  25. // This function prints all the divisors of n,
  26.  
  27. // by finding the lowest divisor, i, and then
  28.  
  29. // rerunning itself on n/i, the remaining quotient.
  30.  
  31. void get_divisors(int n) {
  32.  
  33. int i;
  34.  
  35. double sqrt_of_n = sqrt(n);
  36.  
  37. for (i = 2; i <= sqrt_of_n; i++)
  38.  
  39. if (n % i == 0) { // If i divides n evenly,
  40.  
  41. cout<<i<<",";
  42.  
  43. get_divisors(n / i);
  44.  
  45. return;
  46.  
  47. }
  48.  
  49. // If no divisor is found, then n is prime;
  50.  
  51. // Print n and make no further calls.
  52.  
  53. cout << n;
  54.  
  55. }// end get_divisors()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement