C++ Program To Find All Factors of A Natural Number

Last Updated : 18 Aug, 2026

Factors of a natural number are positive integers that divide the number exactly without leaving a remainder.

  • The optimized approach finds factors in O(√n) time using divisor pairs.
  • The sorted approach prints all factors in increasing order without extra space.
 all divisors of a natural number

Examples:

Input : n = 10
Output: 1 2 5 10

Input: n = 100
Output: 1 2 4 5 10 20 25 50 100

Input: n = 125
Output: 1 5 25 125

Note: Finding all factors is different from finding the prime factors of a number. A factor can be prime or composite.

1. Naive Approach

The naive approach checks every number from 1 to n. If a number divides n exactly, it is a factor and is printed.

C++
#include <iostream>
using namespace std;

// Function to print all factors
void printDivisors(int n)
{
    for (int i = 1; i <= n; i++)
    {
        if (n % i == 0)
            cout << i << " ";
    }
}

int main()
{
    int n = 100;

    cout << "The divisors of " << n << " are: ";
    printDivisors(n);

    return 0;
} 

Output
The divisors of 100 are: 1 2 4 5 10 20 25 50 100 

2. Optimized Approach Using Divisor Pairs

Factors occur in pairs. For example, the factors of 100 can be grouped as:

(1, 100), (2, 50), (4, 25), (5, 20), (10, 10)

For every factor i less than or equal to √n, n / i is also a factor. Therefore, we only need to check numbers up to √n. When i and n / i are equal, the factor should be printed only once.

C++
#include <iostream>
#include <cmath>
using namespace std;

// Function to print all factors
void printDivisors(int n)
{
    for (int i = 1; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            if (i == n / i)
                cout << i << " ";
            else
                cout << i << " " << n / i << " ";
        }
    }
}

int main()
{
    int n = 100;

    cout << "The divisors of " << n << " are: ";
    printDivisors(n);

    return 0;
} 

Output
The divisors of 100 are: 1 100 2 50 4 25 5 20 10 

This approach is significantly faster than checking every number up to n. However, the factors are not printed in sorted order because each factor pair is printed together.

3. Optimized Approach With Sorted Output

We can retain the O(√n) time complexity while printing the factors in increasing order.

The idea is to print the smaller factor while traversing from 1 to √n. Then, after the loop, print the corresponding larger factors in reverse order. This produces all factors in sorted order without storing them in an additional array.

C++
#include <iostream>
using namespace std;

// Function to print all factors in sorted order
void printDivisors(int n)
{
    int i;

    // Print smaller factors
    for (i = 1; i * i <= n; i++)
    {
        if (n % i == 0)
            cout << i << " ";
    }

    // Print larger factors in reverse order
    for (i = i - 1; i >= 1; i--)
    {
        if (n % i == 0 && i != n / i)
            cout << n / i << " ";
    }
}

int main()
{
    int n = 100;

    cout << "The divisors of " << n << " are: ";
    printDivisors(n);

    return 0;
} 

Output
The divisors of 100 are: 1 2 4 5 10 20 25 50 100 

Handling Perfect Squares

For a perfect square, one divisor pair contains two equal values. For example, the factors of 36 include the pair (6, 6). The optimized approach must print 6 only once.

The condition i != n / i prevents the duplicate factor from being printed.

For example:

Factors of 36:
1 2 3 4 6 9 12 18 36

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 36;

    for (int i = 1; i * i <= n; i++) {
        if (n % i == 0) {

            cout << i << " ";

            // Print the paired factor only if it is different
            if (i != n / i)
                cout << n / i << " ";
        }
    }

    return 0;
} 
Try It Yourself
redirect icon

Output
1 36 2 18 3 12 4 9 6 

Explanation: Here, when i = 6, n / i is also 6. Therefore, the condition i != n / i is false, and 6 is printed only once.

Comment