how to get this output?
I need to get the following output
1
2 3
4 5 6
7 8 9 10
This is my code so far
int j=0;
int y = 0;
for (int i = 1; i <= 4; i++){
if( i == y) i += 1;
for (j = 0; j < i; j++){
y = i+j;
System.out.print(y);
}
System.out.println("");
}
And this is the output I get
1
23
4567
Any help?
I don't understand the logic.
what is this for?
if( i == y) i += 1;
by increasing i more than once per loop (including the i++ at for() part), you are decreasing the number of loops. That is why your output is only 3 lines.
Message was edited by:
Icycool
int lineNumber = 1;
int counter = 1;
for (int i = 1; i <= 4; i++)
{
for (int j = 0; j < lineNumber; j++)
{
System.out.print(counter++ + "");
}
System.out.println();
lineNumber++;
}
That should do it.
Duh, you can do it with just one loop:for (int i= 1, j= 1, k= 1; i <= 10; i++) {
System.out.print(i);
if (j++ == k) {
System.out.println();
j= 1;
k++;
}
}
kind regards,
Jos