An Interesting Method to Generate Binary Numbers from 1 to n
Last Updated :
05 Apr, 2023
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
Naive Method: To solve the problem follow the below idea:
A simple method is to run a loop from 1 to n, and call decimal to binary inside the loop.
Code-
C++
#include <bits/stdc++.h>
using namespace std;
void generatePrintBinary( int n)
{
for ( int i=1;i<=n;i++){
string str= "" ;
int temp=i;
while (temp){
if (temp&1){str=to_string(1)+str;}
else {str=to_string(0)+str;}
temp=temp>>1;
}
cout<<str<<endl;
}
}
int main()
{
int n = 10;
generatePrintBinary(n);
return 0;
}
|
Java
import java.util.*;
public class Main {
static void generatePrintBinary( int n) {
for ( int i = 1 ; i <= n; i++) {
String str = "" ;
int temp = i;
while (temp != 0 ) {
if ((temp & 1 ) == 1 ) {
str = "1" + str;
} else {
str = "0" + str;
}
temp = temp >> 1 ;
}
System.out.println(str);
}
}
public static void main(String[] args) {
int n = 10 ;
generatePrintBinary(n);
}
}
|
Python3
def generatePrintBinary(n):
for i in range ( 1 , n + 1 ):
str = ""
temp = i
while temp:
if temp & 1 :
str = "1" + str
else :
str = "0" + str
temp >> = 1
print ( str )
n = 10
generatePrintBinary(n)
|
C#
using System;
class GFG {
static void generatePrintBinary( int n)
{
for ( int i = 1; i <= n; i++) {
string str = "" ;
int temp = i;
while (temp != 0) {
if ((temp & 1) != 0) {
str = "1" + str;
}
else {
str = "0" + str;
}
temp = temp >> 1;
}
Console.WriteLine(str);
}
}
static void Main()
{
int n = 10;
generatePrintBinary(n);
}
}
|
Javascript
function generatePrintBinary(n) {
for (let i = 1; i <= n; i++) {
let str = "" ;
let temp = i;
while (temp) {
if (temp & 1) {
str = "1" + str;
} else {
str = "0" + str;
}
temp = temp >> 1;
}
console.log(str);
}
}
let n = 10;
generatePrintBinary(n);
|
Output-
1
10
11
100
101
110
111
1000
1001
1010
Time Complexity: O(N*logN),N for traversing from 1 to N and logN for decimal to binary conversion
Auxiliary Space: O(1)
Generate Binary Numbers from 1 to n using the queue:
Follow the given steps to solve the problem:
- Create an empty queue of strings
- Enqueue the first binary number “1” to the queue.
- Now run a loop for generating and printing n binary numbers.
- Dequeue and Print the front of queue.
- Append “0” at the end of front item and enqueue it.
- Append “1” at the end of front item and enqueue it.
Thanks to Vivek for suggesting this approach.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void generatePrintBinary( int n)
{
queue<string> q;
q.push( "1" );
while (n--) {
string s1 = q.front();
q.pop();
cout << s1 << "\n" ;
string s2 = s1;
q.push(s1.append( "0" ));
q.push(s2.append( "1" ));
}
}
int main()
{
int n = 10;
generatePrintBinary(n);
return 0;
}
|
Java
import java.util.LinkedList;
import java.util.Queue;
public class GenerateBNo {
static void generatePrintBinary( int n)
{
Queue<String> q = new LinkedList<String>();
q.add( "1" );
while (n-- > 0 ) {
String s1 = q.peek();
q.remove();
System.out.println(s1);
String s2 = s1;
q.add(s1 + "0" );
q.add(s2 + "1" );
}
}
public static void main(String[] args)
{
int n = 10 ;
generatePrintBinary(n);
}
}
|
Python3
def generatePrintBinary(n):
from queue import Queue
q = Queue()
q.put( "1" )
while (n > 0 ):
n - = 1
s1 = q.get()
print (s1)
s2 = s1
q.put(s1 + "0" )
q.put(s2 + "1" )
if __name__ = = "__main__" :
n = 10
generatePrintBinary(n)
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static void generatePrintBinary( int n)
{
LinkedList< string > q = new LinkedList< string >();
q.AddLast( "1" );
while (n-- > 0) {
string s1 = q.First.Value;
q.RemoveFirst();
Console.WriteLine(s1);
string s2 = s1;
q.AddLast(s1 + "0" );
q.AddLast(s2 + "1" );
}
}
public static void Main( string [] args)
{
int n = 10;
generatePrintBinary(n);
}
}
|
Javascript
<script>
function generatePrintBinary(n)
{
var q = [];
q.push( "1" );
while (n--) {
var s1 = q[0];
q.shift();
document.write( s1 + "<br>" );
var s2 = s1;
q.push(s1+ "0" );
q.push(s2+ "1" );
}
}
var n = 10;
generatePrintBinary(n);
</script>
|
Output
1
10
11
100
101
110
111
1000
1001
1010
Time Complexity: O(N)
Auxiliary Space: O(N) as extra space is required in this method
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...