- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

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

# Count of Binary Digit numbers smaller than N in C++

Given an integer N as input. The goal is to find the count of integers that are less than N and represented in Binary form. For example if input N is 12 then numbers less than 12 are 1,10,11 that are binary and contain 0s and 1s as digits. The answer would be 3.

**For Example**

## Input

N=100

## Output

Count of Binary Digit numbers smaller than N are − 4

## Explanation

The Binary numbers less than 100 are − 1, 10, 11, 100

## Input

N=120

## Output

Count of Binary Digit numbers smaller than N are: 7

## Explanation

The Binary numbers less than 100 are : 1, 10, 11, 100, 101, 110, 111

**Approach used in the below program is as follows** −

In this approach we will use an integer vector vec. To this we will first push 1. Now to generate the next binary number we will extract the last number (temp) from vec (Initially 1). Then generate next using: temp*10 and temp*10+1 as binary numbers will always be ( 1,10,11,100,110,111….). Pop numbers from vec and if it is less than N then increment count.

Take an integer N as input.

Function Smaller_N(int N) takes N and returns a count of Binary Digit numbers smaller than N.

Take the initial count as 0.

Take integer vector vec for storing integers that contain 0s and 1s only.

Add 1 to the vector using vec.push_back(1).

Using while loop, traverse vec[ ] and take out last pushed as temp= vec.back(). And remove it from vec.

If temp<=N, then increment count and generate next binary integer as temp*10 and temp*10+1 and add to vec.

At the end of while return count as a result.

## Example

#include <bits/stdc++.h> using namespace std; int Smaller_N(int N){ int count = 0; vector<int> vec; vec.push_back(1); while (!vec.empty()){ int temp = vec.back(); vec.pop_back(); if (temp <= N){ count++; int temp_2 = temp * 10; vec.push_back(temp_2); vec.push_back(temp_2 + 1); } } return count; } int main(){ int N = 1000; cout<<"Count of Binary Digit numbers smaller than N are: "<<Smaller_N(N); return 0; }

## Output

If we run the above code it will generate the following output −

Count of Binary Digit numbers smaller than N are: 8

- Related Articles
- Count numbers (smaller than or equal to N) with given digit sum in C++
- C++ program to count minimum number of binary digit numbers needed to represent n
- Count n digit numbers not having a particular digit in C++
- Count n digit numbers divisible by given number in C++
- Count smaller numbers whose XOR with n produces greater value in C++
- Count of Smaller Numbers After Self in C++
- Count of all N digit numbers such that num + Rev(num) = 10^N - 1 in C++
- Largest number less than N with digit sum greater than the digit sum of N in C++
- Count of n digit numbers whose sum of digits equals to given sum in C++
- Count all possible N digit numbers that satisfy the given condition in C++
- Number of n digit stepping numbers in C++
- An interesting solution to get all prime numbers smaller than n?
- Count numbers from 1 to n that have 4 as a digit in C++
- Minimum numbers which is smaller than or equal to N and with sum S in C++
- Count numbers with difference between number and its digit sum greater than specific value in C++