Question: Recursive Binary Print. Write a recursive method that prints the binary form of a given non-negative integer, with the following specification: public class NPrint {

Recursive Binary Print. Write a recursive method that prints the binary form of a given non-negative integer, with the following specification: public class NPrint { public static void binaryPrint(int n) { // your implementation } } The method prints the value of n as a BINARY number. If n is zero, then a single zero is printed; otherwise no leading zeros are printed in the output. Examples: n=0 Output:0 n=4 Output:100 n=27 Output:11011 Hint: How to convert an int to its binary form? A very intuitive way is to divide the number by 2 in each recursion. In each recursive call, you print either '0' (unless it is a leading zero) if 2 divides n, or '1' otherwise. Requirements: Test Program: you are required to write a test program that tests your NPrint class. Basically, you may want to prompt the user to enter an integer and then invoke the binaryPrint() method in your NPrint class. For the simplicity, you may assume the integer is non-negative. Files to be submitted: NPrint.java and Test.java.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!