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++
// CPP function to generate all binary number
// from 0 to given number n
#include<iostream>
#include<stdlib.h>
using namespace std;
// Maximum length of generated binary number
const int MAX = 100;
// CPP function to generate all binary number
// for given number n
char binaryGenerator(int n)
{
char a[MAX];
for (int i = 0; i <= n; i++) {
// use define function itoa() to convert
// in given base
// a is char[] array where value store
// 2 is base, which convert.
itoa(i, a, 2);
cout << a << endl;
}
}
// Driven program
int main()
{
int n = 10;
binaryGenerator(n);
return 0;
}
Java
// Java function to generate
// all binary number from
// 0 to given number n
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)
{
// add char to
// front of s
s = (x % base) + s;
// integer division
// gives quotient
x = x / base;
}
if (negative)
s = "-" + s;
return s;
}
// function to generate
// all binary number
// for given number n
static void binaryGenerator(int n)
{
System.out.print("0 ");
for (int i = 1; i <= n; i++)
{
// use define function
// itoa() to convert
// in given base
// a is char[] array
// where value store
// 2 is base, which convert.
String a = new String(itoa(i, 2));
System.out.print(a.substring(
0, a.length()) + " ");
}
}
// Driver Code
public static void main(String args[])
{
int n = 10;
binaryGenerator(n);
}
}
// This code is contributed by
// Manish Shaw(manishshaw1)
Python3
# Python function to generate
# all binary number from
# 0 to given number n
def itoa(x, base) :
negative = False
s = ""
if (x == 0) :
return "0"
negative = (x < 0)
if (negative) :
x = -1 * x
while (x != 0) :
# add char to
# front of s
s = str(x % base) + s
# integer division
# gives quotient
x = int(x / base)
if (negative) :
s = "-" . s
return s
# function to generate
# all binary number
# for given number n
def binaryGenerator(n) :
print ("0 ", end = "")
for i in range(1, n + 1) :
# use define function
# itoa() to convert
# in given base
# a is char[] array
# where value store
# 2 is base, which convert.
a = itoa(i, 2)
print ("{} ".format(a[0:]), end = "")
# Driver Code
n = 10
binaryGenerator(n)
# This code is contributed by
# Manish Shaw(manishshaw1)
C#
// C# function to generate
// all binary number from
// 0 to given number n
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);
}
// function to generate
// all binary number
// for given number n
static void binaryGenerator(int n)
{
string a = "";
Console.Write("0 ");
for (int i = 1; i <= n; i++)
{
// use define function
// itoa() to convert
// in given base
// a is char[] array
// where value store
// 2 is base, which convert.
a = itoa(i, 2);
Console.Write(a.Substring(
0, a.Length - 1) + " ");
}
}
// Driver Code
static void Main()
{
int n = 10;
binaryGenerator(n);
}
}
// This code is contributed by
// Manish Shaw(manishshaw1)
JavaScript
<script>
// javascript function to generate
// all binary number from
// 0 to given number n
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){
// add char to
// front of s
s = (x % base).toString() + s
// integer division
// gives quotient
x = Math.floor(x / base)
}
if (negative)
s = "-" + s
return s
}
// function to generate
// all binary number
// for given number n
function binaryGenerator(n){
document.write("0 ")
for(let i=1;i<n + 1;i++) {
// use define function
// itoa() to convert
// in given base
// a is char[] array
// where value store
// 2 is base, which convert.
let a = itoa(i, 2)
document.write(a," ")
}
}
// Driver Code
let n = 10
binaryGenerator(n)
// This code is contributed by shinjanpatra
</script>
PHP
<?php
// PHP function to generate
// all binary number from
// 0 to given number n
function itoa($x, $base)
{
$negative = false;
$s = "";
if ($x == 0)
return "0";
$negative = ($x < 0);
if ($negative)
$x = -1 * $x;
while ($x != 0)
{
// add char to
// front of s
$s = ($x % $base) . $s;
// integer division
// gives quotient
$x = intval($x / $base);
}
if ($negative)
$s = "-" . $s;
return $s;
}
// function to generate
// all binary number
// for given number n
function binaryGenerator($n)
{
echo ("0 ");
for ($i = 1; $i <= $n; $i++)
{
// use define function
// itoa() to convert
// in given base
// a is char[] array
// where value store
// 2 is base, which convert.
$a = itoa($i, 2);
echo (substr($a, 0) . " ");
}
}
// Driver Code
$n = 10;
binaryGenerator($n);
// This code is contributed by
// Manish Shaw(manishshaw1)
?>
Output:
0 1 10 11 100 101 110 111 1000 1001 1010
Time complexity: O(n*n)
Auxiliary space: O(100)
Similar Reads
Generate Binary Numbers from 1 to n Given a number n, write a function that generates and prints all binary numbers with decimal values from 1 to n. Examples: Input: n = 2Output: 1, 10Explanation: The first two non-zero numbers with digits as 0 and 1 onlyInput: n = 5Output: 1, 10, 11, 100, 101[Simple Approach] Using Bit ManipulationWe
5 min read
Generate all the binary strings of N bits Given a positive integer number N. The task is to generate all the binary strings of N bits. These binary strings should be in ascending order.Examples: Input: 2Output:0 00 11 01 1Input: 3Output:0 0 00 0 10 1 00 1 11 0 01 0 11 1 01 1 1Approach: The idea is to try every permutation. For every positio
8 min read
Count total unset bits in all the numbers from 1 to N Given a positive integer N, the task is to count the total number of unset bits in the binary representation of all the numbers from 1 to N. Note that leading zeroes will not be counted as unset bits.Examples: Input: N = 5 Output: 4 IntegerBinary RepresentationCount of unset bits11021013110410025101
4 min read
Generate all binary numbers in range [L, R] with same length Given two positive integer numbers L and R. The task is to convert all the numbers from L to R to binary number. The length of all binary numbers should be same. Examples: Input: L = 2, R = 4Output:010011100Explanation: The binary representation of the numbers: 2 = 10, 3 = 11 and 4 = 100.For the num
6 min read
Generate all binary strings without consecutive 1's Given an integer n, the task is to generate all binary strings of size n without consecutive 1's.Examples: Input : n = 4Output : 0000 0001 0010 0100 0101 1000 1001 1010Input : n = 3Output : 000 001 010 100 101Approach:The idea is to generate all binary strings of length n without consecutive 1's usi
6 min read