# Write a method to generate binary numbers from 1 to n

Category : TECH Author : Ankur Rastogi Date : Wed May 18 2016 Views : 111

Given a number n, Print all the binary number from 1 to n.

### Few Examples can be :-

1) n=3
Binary Numbers will be
1
10
11
2) n=7
Binary Numbers will be
1
10
11
100
101
110
111

In a simple way we can also create a decimal to binary method and call that inside a loop from 1 to n.

But In this we will be solving this with the help of the Queue Data Structure. To see Queue's Basic Implementation in Javascript read here.

### Algo for that Will be :-

|_ Create an empty Queue.
|_ Push first value inside it as 1.
|_ Now run a loop for n times and follow below mentioned steps
|_ Dequeue an item and print the number
|_ Append "0" in the dequeued number and enqueue it in the Queue.
|_ Append "1" in the dequeued number and enqueue it in the Queue.

Now as we know how to solve the problem let's write code now.

### Code :-

"use strict";

//Reference to Queue Class

var Queue = require("../Queue");

var util = require("util");

var NBinaryNumber = function(n){

//Initialize the empty Queue

var queue = new Queue();

//Enqueue element 1

queue.enqueue(1);

//Run a loop for n times

while(n--){

//dequeue an element

var item = queue.dequeue();

//print it

util.log(item.toString());

//Append first 0 and then 1 to a number and enqueue them in Queue.

queue.enqueue(item + "0");

queue.enqueue(item + "1");

}

}

module.exports = NBinaryNumber;

### Test cases :-

"use strict";

var NBinaryNumber = require("../Queue/NBinaryNumber");

describe("Queue Practise Questions", function(){

it("Print Binary Numbers Till 5", function(done){

new NBinaryNumber(5);

done();

});

it("Print Binary Numbers Till 10", function(done){

new NBinaryNumber(10);

done();

});

});

Given a number n, Print all the binary number from 1 to n.

### Few Examples can be :-

1) n=3
Binary Numbers will be
1
10
11
2) n=7
Binary Numbers will be
1
10
11
100
101
110
111

In a simple way we can also create a decimal to binary method and call that inside a loop from 1 to n.

But In this we will be solving this with the help of the Queue Data Structure. To see Queue's Basic Implementation in Javascript read here.

### Algo for that Will be :-

|_ Create an empty Queue.
|_ Push first value inside it as 1.
|_ Now run a loop for n times and follow below mentioned steps
|_ Dequeue an item and print the number
|_ Append "0" in the dequeued number and enqueue it in the Queue.
|_ Append "1" in the dequeued number and enqueue it in the Queue.

Now as we know how to solve the problem let's write code now.

### Code :-

"use strict";

//Reference to Queue Class

var Queue = require("../Queue");

var util = require("util");

var NBinaryNumber = function(n){

//Initialize the empty Queue

var queue = new Queue();

//Enqueue element 1

queue.enqueue(1);

//Run a loop for n times

while(n--){

//dequeue an element

var item = queue.dequeue();

//print it

util.log(item.toString());

//Append first 0 and then 1 to a number and enqueue them in Queue.

queue.enqueue(item + "0");

queue.enqueue(item + "1");

}

}

module.exports = NBinaryNumber;

### Test cases :-

"use strict";

var NBinaryNumber = require("../Queue/NBinaryNumber");

describe("Queue Practise Questions", function(){

it("Print Binary Numbers Till 5", function(done){

new NBinaryNumber(5);

done();

});

it("Print Binary Numbers Till 10", function(done){

new NBinaryNumber(10);

done();

});

});

**Disclaimer:** The above content reflect author’s personal views and do not reflect the views of OYEWIKI. Neither OYEWIKI nor any person/organization acting on its behalf is liable to accept any legal liability/responsibility for any error/mislead in this information or any information available on the website. This website in no way accepts the responsibility for any loss, injury, damage, discomfort or inconvenience caused as a result of reliance on any information provided on this website.

If you want to add more comments to the article or you see any thing incorrect please write a comment below and we will surely get back to you.