Given a positive integer number N. The task is to generate all the binary numbers of N digits. These binary numbers should be in ascending order.
Examples:
Input: 2
Output:
00
01
10
11
Explanation: These 4 are the only binary numbers having 2 digits.Input: 3
Output:
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Approach: For any digit length N, there will be 2N binary numbers.Â
- Therefore traverse from 0 to 2N and convert every number to binary.
- Store each number and print it at the end.
Below is the implementation of the above approach.
C++
// C++ code to implement above approach #include <bits/stdc++.h> using namespace std; Â
// Function to convert number // to binary of N bits vector< int > convertToBinary( int num,                             int length) {     // Vector to store the number     vector< int > bits(length, 0);     if (num == 0) {         return bits;     } Â
    int i = length - 1;     while (num != 0) {         bits[i--] = (num % 2); Â
        // Integer division         // gives quotient         num = num / 2;     }     return bits; } Â
// Function to generate all // N bit binary numbers vector<vector< int > > getAllBinary( int n) { Â Â Â Â vector<vector< int > > binary_nos; Â
    // Loop to generate the binary numbers     for ( int i = 0; i < pow (2, n); i++) {         vector< int > bits =             convertToBinary(i, n);         binary_nos.push_back(bits);     } Â
    return binary_nos; } Â
// Driver code int main() { Â Â Â Â int N = 3; Â Â Â Â vector<vector< int > > binary_nos = Â Â Â Â Â Â Â Â getAllBinary(N); Â Â Â Â for ( int i = 0; i < binary_nos.size(); Â Â Â Â Â Â Â Â Â Â Â i++) { Â Â Â Â Â Â Â Â for ( int j = 0; Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â j < binary_nos[i].size(); j++) Â Â Â Â Â Â Â Â Â Â Â Â cout << binary_nos[i][j]; Â Â Â Â Â Â Â Â cout << endl; Â Â Â Â } Â Â Â Â return 0; } |
Java
// Java code for the above approach import java.io.*; Â
class GFG { Â
  // Function to convert number   // to binary of N bits   static int [] convertToBinary( int num,                                int length)   { Â
    // Vector to store the number     int [] bits = new int [length];     if (num == 0 ) {       return bits;     } Â
    int i = length - 1 ;     while (num != 0 ) {       bits[i--] = (num % 2 ); Â
      // Integer division       // gives quotient       num = num / 2 ;     }     return bits;   } Â
  // Function to generate all   // N bit binary numbers   static int [][] getAllBinary( int n)   {     int [][] binary_nos = new int [( int )Math.pow( 2 ,n)][];     int k = 0 ;          // Loop to generate the binary numbers     for ( int i = 0 ; i < Math.pow( 2 , n); i++) {       int [] bits = convertToBinary(i, n);       binary_nos[k++]= bits;     } Â
    return binary_nos;   } Â
  // Driver code   public static void main (String[] args)   {     int N = 3 ;     int [][] binary_nos = getAllBinary(N);     for ( int i = 0 ; i < binary_nos.length; i++) {       for ( int j = 0 ; j < binary_nos[i].length; j++)         System.out.print(binary_nos[i][j]);       System.out.println();     } Â
  } }; Â
// This code is contributed by Potta Lokesh |
Python3
# Python 3 code to implement above approach Â
# Function to convert number # to binary of N bits def convertToBinary(num, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â length): Â
    # Vector to store the number     bits = [ 0 ] * (length)     if (num = = 0 ):         return bits Â
    i = length - 1     while (num ! = 0 ):         bits[i] = (num % 2 )         i - = 1 Â
        # Integer division         # gives quotient         num = num / / 2 Â
    return bits Â
# Function to generate all # N bit binary numbers def getAllBinary(n): Â
    binary_nos = [] Â
    # Loop to generate the binary numbers     for i in range ( pow ( 2 , n)):         bits = convertToBinary(i, n)         binary_nos.append(bits) Â
    return binary_nos Â
# Driver code if __name__ = = "__main__" : Â
    N = 3     binary_nos = getAllBinary(N)     for i in range ( len (binary_nos)):         for j in range ( len (binary_nos[i])):             print (binary_nos[i][j], end = "")         print () Â
        # This code is contributed by ukasp. |
C#
// C# code for the above approach using System; using System.Collections.Generic; public class GFG { Â
  // Function to convert number   // to binary of N bits   static List< int > convertToBinary( int num, int length) { Â
    // List to store the number     List< int > bits = new List< int >();     if (num == 0) {       bits.Add(0);       bits.Add(0);       bits.Add(0);       return bits;     } Â
    int i = length - 1;     while (num != 0) {       bits.Add(num % 2); Â
      // int division       // gives quotient       num = num / 2;     }     while (bits.Count<3)       bits.Add(0);     return bits;   } Â
  // Function to generate all   // N bit binary numbers   static List<List< int >> getAllBinary( int n) {     List<List< int >> binary_nos = new List<List< int >>(); Â
    // Loop to generate the binary numbers     for ( int i = 0; i < Math.Pow(2, n); i++) {       List< int > bits = convertToBinary(i, n);       binary_nos.Add(bits);     } Â
    return binary_nos;   } Â
  // Driver code   public static void Main(String[] args) {     int N = 3;     List<List< int >> binary_nos = getAllBinary(N);     foreach ( var st in binary_nos){       foreach ( var s in st){         Console.Write(s);       }       Console.WriteLine();     }   } } Â
// This code is contributed by Rajput-Ji |
Javascript
<script> Â Â Â Â // JavaScript code to implement above approach Â
    // Function to convert number     // to binary of N bits     const convertToBinary = (num, length) => {         // Vector to store the number         let bits = new Array(length).fill(0);         if (num == 0) {             return bits;         } Â
        let i = length - 1;         while (num != 0) {             bits[i--] = (num % 2); Â
            // Integer division             // gives quotient             num = parseInt(num / 2);         }         return bits;     } Â
    // Function to generate all     // N bit binary numbers     const getAllBinary = (n) => {         let binary_nos = []; Â
        // Loop to generate the binary numbers         for (let i = 0; i < parseInt(Math.pow(2, n)); i++) {             let bits = convertToBinary(i, n);             binary_nos.push(bits);         } Â
        return binary_nos;     } Â
    // Driver code Â
    let N = 3;     let binary_nos = getAllBinary(N);     for (let i = 0; i < binary_nos.length;         i++) {         for (let j = 0;             j < binary_nos[i].length; j++)             document.write(binary_nos[i][j]);         document.write( "<br/>" );     } Â
    // This code is contributed by rakeshsahni Â
</script> |
Â
Â
000 001 010 011 100 101 110 111
Â
Time Complexity: O(2N)
Auxiliary Space: O(2N)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!