Recursive program to print all numbers less than N which consist of digits 1 or 3 only
Given an integer N, the task is to print all the numbers ? N which have their digits as only 1 or 3.
Examples:
Input: N = 10
Output: 3 1
Input: N = 20
Output: 13 11 3 1
Approach:
- 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:
C++
#include <iostream>
using namespace std;
void printNumbers( int N)
{
bool flag = 1;
int x = N;
if (N > 0) {
while (x > 0 && flag == 1) {
int digit = x % 10;
if (digit != 1 && digit != 3)
flag = 0;
x = x / 10;
}
if (flag == 1)
cout << N << " " ;
printNumbers(N - 1);
}
}
int main()
{
int N = 20;
printNumbers(N);
return 0;
}
|
Java
class GFG
{
static void printNumbers( int N)
{
int flag = 1 ;
int x = N;
if (N > 0 )
{
while (x > 0 && flag == 1 )
{
int digit = x % 10 ;
if (digit != 1 && digit != 3 )
{
flag = 0 ;
}
x = x / 10 ;
}
if (flag == 1 ) {
System.out.print(N + " " );
}
printNumbers(N - 1 );
}
}
public static void main(String[] args)
{
int N = 20 ;
printNumbers(N);
}
}
|
Python3
def printNumbers(N):
flag = 1
x = N
if (N > 0 ):
while (x > 0 and flag = = 1 ):
digit = x % 10
if (digit ! = 1 and digit ! = 3 ):
flag = 0
x = x / / 10
if (flag = = 1 ):
print (N, end = " " )
printNumbers(N - 1 )
if __name__ = = '__main__' :
N = 20
printNumbers(N)
|
C#
using System;
class GFG
{
static void printNumbers( int N)
{
int flag = 1;
int x = N;
if (N > 0)
{
while (x > 0 && flag == 1)
{
int digit = x % 10;
if (digit != 1 && digit != 3)
flag = 0;
x = x / 10;
}
if (flag == 1)
Console.Write(N + " " );
printNumbers(N - 1);
}
}
public static void Main()
{
int N = 20;
printNumbers(N);
}
}
|
PHP
<?php
function printNumbers( $N )
{
$flag = 1;
$x = $N ;
if ( $N > 0)
{
while ((int) $x > 0 && $flag == 1)
{
$digit = $x % 10;
if ( $digit != 1 && $digit != 3)
$flag = 0;
$x = $x / 10;
}
if ( $flag == 1)
{
echo $N ;
echo " " ;
}
printNumbers( $N - 1);
}
}
$N = 20;
printNumbers( $N );
?>
|
Javascript
<script>
function printNumbers(N)
{
let flag = 1;
let x = N;
if (N > 0)
{
while (x > 0 && flag == 1)
{
let digit = x % 10;
if (digit != 1 && digit != 3)
flag = 0;
x = parseInt(x / 10, 10);
}
if (flag == 1)
document.write(N + " " );
printNumbers(N - 1);
}
}
let N = 20;
printNumbers(N);
</script>
|
Time complexity: O(N*logN)
Auxiliary space: O(N) for recursive stack space.
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.
Last Updated :
16 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...