Given a number **N**, the task is to print all the factors of N using recursion.

**Examples:**

Input:N = 16

Output:1 2 4 8 16

Explanation:

1, 2, 4, 8, 16 are the factors of 16. A factor is a number which divides the number completely.

Input:N = 8

Output:1 2 4 8

**Approach:** The idea is to create a function that takes 2 arguments. The function is recursively called from 1 to N and in every call, if the number is a factor of N, then it is printed. The recursion will stop when the number exceeds N.

Below is the implementation of the above approach:

## C++

`// C++ program to find all the factors ` `// of a number using recursion ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Recursive function to ` `// print factors of a number ` `void` `factors(` `int` `n, ` `int` `i) ` `{ ` ` ` `// Checking if the number is less than N ` ` ` `if` `(i <= n) { ` ` ` `if` `(n % i == 0) { ` ` ` `cout << i << ` `" "` `; ` ` ` `} ` ` ` ` ` `// Calling the function recursively ` ` ` `// for the next number ` ` ` `factors(n, i + 1); ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 16; ` ` ` `factors(N, 1); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find all the factors ` `// of a number using recursion ` ` ` `class` `GFG { ` ` ` ` ` `// Recursive function to ` ` ` `// print factors of a number ` ` ` `static` `void` `factors(` `int` `n, ` `int` `i) ` ` ` `{ ` ` ` ` ` `// Checking if the number is less than N ` ` ` `if` `(i <= n) { ` ` ` `if` `(n % i == ` `0` `) { ` ` ` `System.out.print(i + ` `" "` `); ` ` ` `} ` ` ` ` ` `// Calling the function recursively ` ` ` `// for the next number ` ` ` `factors(n, i + ` `1` `); ` ` ` `} ` ` ` `} ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `N = ` `16` `; ` ` ` `factors(N, ` `1` `); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find all the factors ` `# of a number using recursion ` ` ` `# Recursive function to ` `# prfactors of a number ` `def` `factors(n, i): ` ` ` ` ` `# Checking if the number is less than N ` ` ` `if` `(i <` `=` `n): ` ` ` `if` `(n ` `%` `i ` `=` `=` `0` `): ` ` ` `print` `(i, end ` `=` `" "` `); ` ` ` ` ` `# Calling the function recursively ` ` ` `# for the next number ` ` ` `factors(n, i ` `+` `1` `); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `N ` `=` `16` `; ` ` ` `factors(N, ` `1` `); ` ` ` `# This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find all the factors ` `// of a number using recursion ` ` ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Recursive function to ` ` ` `// print factors of a number ` ` ` `static` `void` `factors(` `int` `n, ` `int` `i) ` ` ` `{ ` ` ` ` ` `// Checking if the number is less than N ` ` ` `if` `(i <= n) { ` ` ` `if` `(n % i == 0) { ` ` ` `Console.WriteLine(i + ` `" "` `); ` ` ` `} ` ` ` ` ` `// Calling the function recursively ` ` ` `// for the next number ` ` ` `factors(n, i + 1); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 16; ` ` ` `factors(n, 1); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1 2 4 8 16

**Time Complexity:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find number of factors of N when location of its two factors whose product is N is given
- Maximum number of prime factors a number can have with exactly x factors
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Check if a number exists having exactly N factors and K prime factors
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Efficient program to print all prime factors of a given number
- C Program for efficiently print all prime factors of a given number
- Java Program for efficiently print all prime factors of a given number
- Find all Factors of Large Perfect Square Natural Number in O(sqrt(sqrt(N))
- Queries on sum of odd number digit sums of all the factors of a number
- C Program to reverse the digits of a number using recursion
- Number of factors of very large number N modulo M where M is any prime number
- Sum of all the factors of a number
- Check if all Prime factors of number N are unique or not
- Program to print factors of a number in pairs
- Efficient program to print the number of factors of n numbers
- Generating all possible Subsequences using Recursion
- Move all occurence of letter 'x' from the string s to the end using Recursion
- Program for length of a string using recursion
- Program to check if an array is palindrome or not using Recursion

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.