From the course: Nail Your Java Interview

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Solution: Generate binary numbers

Solution: Generate binary numbers - Java Tutorial

From the course: Nail Your Java Interview

Start my 1-month free trial

Solution: Generate binary numbers

(upbeat music) - [Instructor] Let's create a program that print outs a given number of binary numbers. First, we set up our test cases. We have a function called printBinary that does the work for us. We give it positive inputs and negative inputs. Let's take a look at that printBinary function. Here, we have a function called printBinary that takes in an integer N. It returns void, since the goal of the function is not to return a string, but rather print out the binary numbers. Inside the function, we check if the input is valid. If the input is less than or equal to zero, we claim there's nothing to print, and exit the function with a return statement. Now for the bulk of the work. The goal of the function is to print out the first N binary numbers. One way to achieve this is by doing a for loop and converting each number, starting at one, from the base 10 system or the decimal system to the binary system, as a…

Contents