Given a number n, write a function that generates and prints all binary numbers with decimal values from 1 to n.

Examples:

Input: n = 2 Output: 1, 10 Input: n = 5 Output: 1, 10, 11, 100, 101

A simple method is to run a loop from 1 to n, call decimal to binary inside the loop.

Following is an interesting method that uses queue data structure to print binary numbers. Thanks to Vivek for suggesting this approach.

1) Create an empty queue of strings

2) Enqueue the first binary number “1” to queue.

3) Now run a loop for generating and printing n binary numbers.

……a) Dequeue and Print the front of queue.

……b) Append “0” at the end of front item and enqueue it.

……c) Append “1” at the end of front item and enqueue it.

Following is implementation of above algorithm.

## C++

// C++ program to generate binary numbers from 1 to n #include <iostream> #include <queue> using namespace std; // This function uses queue data structure to print binary numbers void generatePrintBinary(int n) { // Create an empty queue of strings queue<string> q; // Enqueue the first binary number q.push("1"); // This loops is like BFS of a tree with 1 as root // 0 as left child and 1 as right child and so on while (n--) { // print the front of queue string s1 = q.front(); q.pop(); cout << s1 << "\n"; string s2 = s1; // Store s1 before changing it // Append "0" to s1 and enqueue it q.push(s1.append("0")); // Append "1" to s2 and enqueue it. Note that s2 contains // the previous front q.push(s2.append("1")); } } // Driver program to test above function int main() { int n = 10; generatePrintBinary(n); return 0; }

## Java

//Java program to generate binary numbers from 1 to n import java.util.LinkedList; import java.util.Queue; public class GenerateBNo { // This function uses queue data structure to print binary numbers static void generatePrintBinary(int n) { // Create an empty queue of strings Queue<String> q = new LinkedList<String>(); // Enqueue the first binary number q.add("1"); // This loops is like BFS of a tree with 1 as root // 0 as left child and 1 as right child and so on while(n-- > 0) { // print the front of queue String s1 = q.peek(); q.remove(); System.out.println(s1); // Store s1 before changing it String s2 = s1; // Append "0" to s1 and enqueue it q.add(s1 + "0"); // Append "1" to s2 and enqueue it. Note that s2 contains // the previous front q.add(s2 + "1"); } } // Driver program to test above function public static void main(String[] args) { int n=10; generatePrintBinary(n); } } //This code is contributed by Sumit Ghosh

## Python

# Python program to generate binary numbers from # 1 to n # This function uses queu data structure to print binary numbers def generatePrintBinary(n): # Create an empty queue from Queue import Queue q = Queue() # Enqueu the first binary number q.put("1") # This loop is like BFS of a tree with 1 as root # 0 as left child and 1 as right child and so on while(n>0): n-= 1 # Print the front of queue s1 = q.get() print s1 s2 = s1 # Store s1 before changing it # Append "0" to s1 and enqueue it q.put(s1+"0") # Append "1" to s2 and enqueue it. Note that s2 # contains the previous front q.put(s2+"1") # Driver program to test above function n = 10 generatePrintBinary(n) # This code is contributed by Nikhil Kumar Singh(nickzuck_007)

Output:

1 10 11 100 101 110 111 1000 1001 1010

This article is contributed by **Abhishek**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above