Program to find all Factors of a Number using recursion

30 Jan, 2022
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++
Java
Python3
C#
Javascript
// 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 numbervoid 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 codeint main(){    int N = 16;    factors(N, 1);}
// 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);    }}
# Python3 program to find all the factors# of a number using recursion # Recursive function to# prfactors of a numberdef 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 codeif __name__ == '__main__':    N = 16;    factors(N, 1); # This code is contributed by Rajput-Ji
// 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);    }}
<script> // Javascript program to find all the factors// of a number using recursion // Recursive function to// print factors of a numberfunction factors(n, i){    // Checking if the number is less than N    if (i <= n) {        if (n % i == 0) {            document.write(i + " ");        }         // Calling the function recursively        // for the next number        factors(n, i + 1);    }} // Driver codevar N = 16;factors(N, 1); </script>
1 2 4 8 16
 
Time Complexity: O(N)
Auxiliary Space: O(N) 
Rajput-Ji
rutvik_56
subhammahato348
Algorithms-Recursion
factor
Mathematical
Recursion
School Programming
Mathematical
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Find minimum number of coins that make a given value
Minimum number of jumps to reach end
Algorithm to solve Rubik's Cube
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Recursion
Program for Tower of Hanoi
Backtracking | Introduction
Print all subsequences of a string