Generate all the binary number from 0 to n
Last Updated :
09 Aug, 2022
Given a positive integer number n generate all the binary number from 0 to n. Examples:
Input : 5
Output : 0 1 10 11 100 101
Binary numbers are 0(0), 1(1), 2(10),
3(11), 4(100) and 5(101).
Input : 10
Output : 0 1 10 11 100 101 110
111 1000 1001 1010
This program simple use predefined function (itoa() in C++) which convert in which base you want. so simple used these function and convert binary number it is consist three value.
C++
#include<iostream>
#include<stdlib.h>
using namespace std;
const int MAX = 100;
char binaryGenerator( int n)
{
char a[MAX];
for ( int i = 0; i <= n; i++) {
itoa(i, a, 2);
cout << a << endl;
}
}
int main()
{
int n = 10;
binaryGenerator(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static String itoa( int x,
int base)
{
boolean negative = false ;
String s = "" ;
if (x == 0 )
return "0" ;
negative = (x < 0 );
if (negative)
x = - 1 * x;
while (x != 0 )
{
s = (x % base) + s;
x = x / base;
}
if (negative)
s = "-" + s;
return s;
}
static void binaryGenerator( int n)
{
System.out.print( "0 " );
for ( int i = 1 ; i <= n; i++)
{
String a = new String(itoa(i, 2 ));
System.out.print(a.substring(
0 , a.length()) + " " );
}
}
public static void main(String args[])
{
int n = 10 ;
binaryGenerator(n);
}
}
|
Python3
def itoa(x, base) :
negative = False
s = ""
if (x = = 0 ) :
return "0"
negative = (x < 0 )
if (negative) :
x = - 1 * x
while (x ! = 0 ) :
s = str (x % base) + s
x = int (x / base)
if (negative) :
s = "-" . s
return s
def binaryGenerator(n) :
print ( "0 " , end = "")
for i in range ( 1 , n + 1 ) :
a = itoa(i, 2 )
print ( "{} " . format (a[ 0 :]), end = "")
n = 10
binaryGenerator(n)
|
C#
using System;
class GFG
{
static String itoa( int n,
int radix)
{
if (0 == n)
return "0" ;
var index = 10;
var buffer = new char [1 + index];
var xlat = "0123456789abcdefghijklmnopqrstuvwxyz" ;
for ( int r = Math.Abs(n), q; r > 0; r = q)
{
q = Math.DivRem(r, radix, out r);
buffer[index -= 1] = xlat[r];
}
if (n < 0)
{
buffer[index -= 1] = '-' ;
}
return new String(buffer, index,
buffer.Length -
index);
}
static void binaryGenerator( int n)
{
string a = "" ;
Console.Write( "0 " );
for ( int i = 1; i <= n; i++)
{
a = itoa(i, 2);
Console.Write(a.Substring(
0, a.Length - 1) + " " );
}
}
static void Main()
{
int n = 10;
binaryGenerator(n);
}
}
|
Javascript
<script>
function itoa(x, base){
let negative = false
let s = ""
if (x == 0)
return "0"
negative = (x < 0)
if (negative)
x = -1 * x
while (x != 0){
s = (x % base).toString() + s
x = Math.floor(x / base)
}
if (negative)
s = "-" + s
return s
}
function binaryGenerator(n){
document.write( "0 " )
for (let i=1;i<n + 1;i++) {
let a = itoa(i, 2)
document.write(a, " " )
}
}
let n = 10
binaryGenerator(n)
</script>
|
PHP
<?php
function itoa( $x , $base )
{
$negative = false;
$s = "" ;
if ( $x == 0)
return "0" ;
$negative = ( $x < 0);
if ( $negative )
$x = -1 * $x ;
while ( $x != 0)
{
$s = ( $x % $base ) . $s ;
$x = intval ( $x / $base );
}
if ( $negative )
$s = "-" . $s ;
return $s ;
}
function binaryGenerator( $n )
{
echo ( "0 " );
for ( $i = 1; $i <= $n ; $i ++)
{
$a = itoa( $i , 2);
echo ( substr ( $a , 0) . " " );
}
}
$n = 10;
binaryGenerator( $n );
?>
|
Output:
0 1 10 11 100 101 110 111 1000 1001 1010
Time complexity: O(n*n)
Auxiliary space: O(100)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...