Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- void get_divisors(int n);
- int main() {
- int n = 0;
- cout << "Enter a number and press ENTER: ";
- cin >> n;
- get_divisors(n);
- cout << endl;
- return 0;
- }// end main
- // Get divisors function
- // This function prints all the divisors of n,
- // by finding the lowest divisor, i, and then
- // rerunning itself on n/i, the remaining quotient.
- void get_divisors(int n) {
- int i;
- double sqrt_of_n = sqrt(n);
- for (i = 2; i <= sqrt_of_n; i++)
- if (n % i == 0) { // If i divides n evenly,
- cout<<i<<",";
- get_divisors(n / i);
- return;
- }
- // If no divisor is found, then n is prime;
- // Print n and make no further calls.
- cout << n;
- }// end get_divisors()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement