Given a number N, the task is to print all the factors of N using recursion.
Input: N = 16
Output: 1 2 4 8 16
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:
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.