- Related Questions & Answers
- Count number of binary strings of length N having only 0’s and 1’s in C++
- Maximum length of segments of 0’s and 1’s in C++
- 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++
- 1’s and 2’s complement of a Binary Number?
- Count the number of 1’s and 0’s in a binary array using STL in C++
- Binary representation of next greater number with same number of 1’s and 0’s in C Program?
- Segregate 0’s and 1’s in an array list using Python?
- Maximum 0’s between two immediate 1’s in binary representation in C++
- Maximum sub-matrix area having count of 1’s one more than count of 0’s in C++
- Previous number same as 1’s complement in C++
- Python map function to find the row with the maximum number of 1’s
- C/C++ Program to Count number of binary strings without consecutive 1’s?
- Program to Count number of binary strings without consecutive 1’s in C/C++?
- Repeated sum of Number’s digits in JavaScript
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors in C++

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

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.

#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); }

Number of values using 0s and 1s: 7

Advertisements