Given a number **N**. The task is to find the number of integers from 1 to n which contains digits 0’s and 1’s only.

**Examples:**

Input :N = 15Output :3Explanation :1, 10, 11 are such integers.Input :N = 120Output :7Explanation :1, 10, 11, 100, 101, 110, 111 are such integers.

**Approach**: An efficient approach is to build integers which contain 1’s and 0’s only using a recursive function starting from the number 1. For each number check whether it is less than **n **or not.

Below is the implementation of the above approach:

## C++

`// CPP program to find the number of integers ` `// from 1 to n which contains digits 0's and 1's only ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the number of integers ` `// from 1 to n which contains 0's and 1's only ` `int` `countNumbers(` `int` `x, ` `int` `n) ` `{ ` ` ` `// If number is greater than n ` ` ` `if` `(x > n) ` ` ` `return` `0; ` ` ` ` ` `// otherwise add count this number and ` ` ` `// call two functions ` ` ` `return` `1 + countNumbers(x * 10, n) + countNumbers(x * 10 + 1, n); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 120; ` ` ` ` ` `cout << countNumbers(1, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the number of integers ` `// from 1 to n which contains digits 0's and 1's only ` `class` `GFG ` `{ ` ` ` `// Function to find the number of integers ` `// from 1 to n which contains 0's and 1's only ` `static` `int` `countNumbers(` `int` `x, ` `int` `n) ` `{ ` ` ` `// If number is greater than n ` ` ` `if` `(x > n) ` ` ` `return` `0` `; ` ` ` ` ` `// otherwise add count this number and ` ` ` `// call two functions ` ` ` `return` `1` `+ countNumbers(x * ` `10` `, n) + countNumbers(x * ` `10` `+ ` `1` `, n); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `n = ` `120` `; ` ` ` ` ` `System.out.println(countNumbers(` `1` `, n)); ` `} ` `} ` ` ` `// This code is contributed by chandan_jnu ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the number of ` `# integers from 1 to n which contains ` `# digits 0's and 1's only ` ` ` `# Function to find the number of integers ` `# from 1 to n which contains 0's and 1's only ` `def` `countNumbers(x, n): ` ` ` ` ` `# If number is greater than n ` ` ` `if` `x > n : ` ` ` `return` `0` ` ` ` ` `# otherwise add count this number and ` ` ` `# call two functions ` ` ` `return` `(` `1` `+` `countNumbers(x ` `*` `10` `, n) ` `+` ` ` `countNumbers(x ` `*` `10` `+` `1` `, n)) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `120` `; ` ` ` ` ` `print` `(countNumbers(` `1` `, n)); ` ` ` `# This code is contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the number of integers ` `// from 1 to n which contains digits 0's and 1's only ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the number of integers ` `// from 1 to n which contains 0's and 1's only ` `static` `int` `countNumbers(` `int` `x, ` `int` `n) ` `{ ` ` ` `// If number is greater than n ` ` ` `if` `(x > n) ` ` ` `return` `0; ` ` ` ` ` `// otherwise add count this number and ` ` ` `// call two functions ` ` ` `return` `1 + countNumbers(x * 10, n) + ` ` ` `countNumbers(x * 10 + 1, n); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 120; ` ` ` ` ` `Console.WriteLine(countNumbers(1, n)); ` `} ` `} ` ` ` `// This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the number of ` `// integers from 1 to n which contains ` `// digits 0's and 1's only ` ` ` `// Function to find the number of integers ` `// from 1 to n which contains 0's and 1's only ` `function` `countNumbers(` `$x` `, ` `$n` `) ` `{ ` ` ` `// If number is greater than n ` ` ` `if` `(` `$x` `> ` `$n` `) ` ` ` `return` `0; ` ` ` ` ` `// otherwise add count this number and ` ` ` `// call two functions ` ` ` `return` `1 + countNumbers(` `$x` `* 10, ` `$n` `) + ` ` ` `countNumbers(` `$x` `* 10 + 1, ` `$n` `); ` `} ` ` ` `// Driver code ` `$n` `= 120; ` ` ` `echo` `(countNumbers(1, ` `$n` `)); ` ` ` `// This code is contributed ` `// by Code_Mech. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

7

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:

- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Number of integers in a range [L, R] which are divisible by exactly K of it's digits
- Recursive program to print all numbers less than N which consist of digits 1 or 3 only
- Minimum number with digits as 4 and 7 only and given sum
- Find the average of k digits from the beginning and l digits from the end of the given number
- Length of the smallest number which is divisible by K and formed by using 1's only
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Check if N is divisible by a number which is composed of the digits from the set {A, B}
- Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
- Find the smallest number whose digits multiply to a given number n
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Smallest multiple of 3 which consists of three given non-zero digits
- Number of ways to divide a given number as a set of integers in decreasing order
- Count of possible permutations of a number represented as a sum of 2's, 4's and 6's only
- Find largest number smaller than N with same set of digits
- Find the kth smallest number with sum of digits as m
- Find n positive integers that satisfy the given equations
- Find all the possible remainders when N is divided by all positive integers from 1 to N+1

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.