From the course: C# Hands-on Practice with Data-Structures

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

CoderPad Solution: Generate binary numbers

CoderPad Solution: Generate binary numbers - C# Tutorial

From the course: C# Hands-on Practice with Data-Structures

CoderPad Solution: Generate binary numbers

Let's create a program that returns the first n binary numbers. If we look at the numbers we're supposed to generate, you'll notice a pattern. We have a number, in this case, one. And then that same number with a zero appended to it and then a one appended to it. Then look at the second number. After the items associated with the first number have been shown, we have the second number with a zero appended to it and then a one appended to it. This pattern follows throughout the rest of the binary numbers. We can leverage this pattern and use our queue to generate the first n binary numbers in our code. Let's implement this algorithm. We'll start off with validating the input and checking if it's greater than zero. If it's equal to or less than zero, we'll return an empty array. If the input is greater than one, we'll create a result array to return in a queue that will help us generate the next item to be printed. Let's add our import for the queue. Now these structures will hold…

Contents