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 - Java Tutorial
From the course: Nail Your Java Interview
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
Leveraging stacks as a data structure4m 36s
-
Using queues in technical interviews4m 5s
-
Mastering hash-based structures4m 51s
-
Challenge: Generate binary numbers1m 19s
-
Solution: Generate binary numbers6m 35s
-
Challenge: Matching parentheses1m 5s
-
Solution: Matching parentheses3m 51s
-
Challenge: Find most repeated word2m 16s
-
Solution: Find most repeated word3m 28s
-
-
-