I know the point is probably to use recursion, but recursion in this case is a horrible solution. Here's a solution that is more efficient (though it very likely uses a loop in Arrays.fill
!)
public static void printNX(int n)
{
char[] c = new char[n];
Arrays.fill(c, 'x');
System.out.println(new String(c));
}
Of course, it's possible that Arrays.fill
is calls into native code which is optimized to use an efficient instruction for filling the array and avoids a loop. But you never know.
I don't necessarily agree that using recursion "isn't looping"; all this is doing is thrashing the stack; the the CPU will still technically loop by continually jumping back to the top of the recursive function.