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.
The issue Im having with this is I don't know how I would print the zeros and ones, since n is the only parameter I can have. I also cannot use a for loop and put my recursive call in the loop, so Im sorta stuck. this is what I have so far:
public static void countBinary(int n){
if (n < 0) {
throw new IllegalArgumentException();
}if(n == 0){
System.out.print("");
}else{
countBinary(n - 1);
System.out.println(n ); // I tried doing n + "0" and + "1" did not work
countBinary(n - 1 );
System.out.print(n );
// store += n;
}
}
And here is my output for countBinary(2):
1
12
1
12
When it should be this:
00
01
10
11
I am getting the right amount of "levels" for every other line which is strange, but Im really stuck
Note: this is NOT homework, simply practice. THanks!