Java – Print multiple lines output in java without using a new line character

java

this is one of the interview question. I am supposed to print multiple lines of output on command line, without using the newline(\n) character in java. I tried googling for this, didn't find appropriate answers. If i am printing 5 numbers, then it should print in the following fashion. But I am not supposed to use the newline character nor loops either. I have to print this using a single println() statement. Can you give me some ideas ? Thanks !

1
2
3
4
5

Best Solution

You can do it recursively:

public void foo(int currNum) {
  if (currNum > 5) 
    return;
  println(currNum);
  foo(currNum + 1);
}

Then you are only using a single println and you aren't using a for or while loop.