VOOZH about

URL: https://www.geeksforgeeks.org/dsa/generate-binary-number-0-n/

⇱ Generate all the binary number from 0 to n - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Generate all the binary number from 0 to n

Last Updated : 9 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. 

Output:

 0 1 10 11 100 101 110 111 1000 1001 1010

Time complexity: O(n*n)

Auxiliary space: O(100)

Comment
Article Tags: