• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
July 08, 2022 |4.1K Views

An Interesting Method to generate binary numbers

  Share  2 Likes
Description
Discussion

Following is an interesting method that uses queue data structure to print binary numbers. Thanks to Vivek for suggesting this approach.

Create an empty queue of strings 
Enqueue the first binary number “1” to 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.


An Interesting Method to generate binary numbers : https://www.geeksforgeeks.org/interesting-method-generate-binary-numbers-1-n/