- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find the number of integers from 1 to n which contains digits 0’s and 1’s only in C++

Suppose, we have a number n. Our task is to find the number of integers from 1 to n, which contains digits 0s and 1s only. So if n = 15, then output will be. As the numbers are 1, 10, 11

To solve this, we will create integers using 0s and 1s using recursive function. Following code will help us to understand this better.

## Example

#include<iostream> using namespace std; int numberOfValues(int p, int n) { if (p > n) return 0; return 1 + numberOfValues(p * 10, n) + numberOfValues(p * 10 + 1, n); } int main() { int n = 120; cout << "Number of values using 0s and 1s: " << numberOfValues(1, n); }

## Output

Number of values using 0s and 1s: 7

- Related Questions & Answers
- Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0’s in C++
- Count number of binary strings of length N having only 0’s and 1’s in C++
- Find the index of first 1 in a sorted array of 0's and 1's in C++
- Construct DFA of alternate 0’s and 1’s
- Find the Pattern of 1’s inside 0’s using C++
- Count subarrays consisting of only 0’s and only 1’s in a binary array in C++
- Count subarrays with equal number of 1’s and 0’s in C++
- Largest number with binary representation is m 1’s and m-1 0’s in C++
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors in C++
- Maximum length of segments of 0’s and 1’s in C++
- Check if the given decimal number has 0 and 1 digits only in Python
- Count Numbers with N digits which consists of even number of 0's in C++
- Count Numbers with N digits which consists of odd number of 0's in C++
- Sort an arrays of 0’s, 1’s and 2’s using C++
- Sort an arrays of 0’s, 1’s and 2’s using Java

Advertisements