Given an integer N, the task is to print all the numbers ≤ N which have their digits as only 1 or 3.
Input: N = 10
Output: 3 1
Input: N = 20
Output: 13 11 3 1
- First check if the number is greater than 0. If yes then proceed further, else program is terminated.
- Check for the presence of digits 1 or 3 at each place of the number.
- If we find 1 or 3 at every place of the number then print the number. Now, check for the next number by using a recursive call for a number one less than the current number.
Below is the implementation of the above approach:
13 11 3 1
Note that the idea of this post to explain a recursive solution there exist a better approach to solve this problem. We can use queue to solve this efficiently. Please refer Count of Binary Digit numbers smaller than N for details of efficient approach.
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.
- Recursive program to print all subsets with given sum
- Recursive program to print formula for GCD of n integers
- Recursive Program to print multiplication table of a number
- Print numbers with digits 0 and 1 only such that their sum is N
- Program to print first N Stepping numbers
- Program to print numbers from N to 1 in reverse order
- Program to Print Pyramid Pattern using numbers
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Print the last k nodes of the linked list in reverse order | Recursive approach
- Recursive program to generate power set
- C++ Program for Recursive Bubble Sort
- Recursive Program for Binary to Decimal
- Recursive program to find the Sum of the series 1 - 1/2 + 1/3 - 1/4 ... 1/N
- Recursive program for prime number
- Recursive program to find all Indices of a Number
- Recursive program to replace all occurrences of pi with 3.14 in a given string
- Program to reverse a string (Iterative and Recursive)
- Recursive program to insert a star between pair of identical characters
- Count rows in a matrix that consist of same element