Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method countBinary that accepts an integer n as a parameter and that pri

ID: 642491 • Letter: W

Question

Write a method countBinary that accepts an integer n as a parameter and that prints all binary numbers that have n digits in ascending order, printing each value on a separate line. All n digits should be shown for all numbers, including leading zeros if necessary. You may assume that n is non-negative. If n is 0, a blank line of output should be produced. Do not use a loop in your solution; implement it recursively.

Hint: It may help to define a private helper method that accepts different parameters than the original method. In particular, consider building up a set of characters as a String for eventual printing.

Explanation / Answer

public static int binarynumber(int n) {
String binary = Integer.toString(n, 2);
int number = 0;
for (int i = 0; i < binary.length(); i++) {
if (binary.charAt(i) == '0') {
number++;
}
}
return number;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote