Search code examples
javastringloopsnested-loops

Stuck in Indenting spaces according to numbers using a nested loop


I need some help. I want my program to print different numbers according to the user's input and indent that number with that number of spaces as well. Must be using a nested loop.

Expected Output: (lets say user types 3)

0  
 1
  2
   3

for simplicity I'm only showing you what I did in the nested for loop and ignoring the user's input portion.

What I did:

for (i=0; i<=userNum; i++) 
  {
     for (j=0; j<=i; j++) 
     {
        ? (I think here is the problem as I don't know how to indent by that no of spaces)
     }
     System.out.println(i);
  }  

Thanks in advance!


Solution

  • Inside the loop you should use

    System.out.print(" ");