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?

[744 byte] By [mathewronda] at [2007-11-26 19:54:43]
# 1

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

Icycoola at 2007-7-9 22:47:20 > top of Java-index,Java Essentials,Java Programming...
# 2
use System.out.println if you findnumber of System.out.print equals with lineno
p_epia at 2007-7-9 22:47:20 > top of Java-index,Java Essentials,Java Programming...
# 3
Thats where the logic is wrong and I need your help to correct me.
mathewronda at 2007-7-9 22:47:20 > top of Java-index,Java Essentials,Java Programming...
# 4

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.

java4life87a at 2007-7-9 22:47:20 > top of Java-index,Java Essentials,Java Programming...
# 5

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

JosAHa at 2007-7-9 22:47:20 > top of Java-index,Java Essentials,Java Programming...
# 6
Try this please...int line=1;for(int i=1; i<=10; ){for(int x=0; x<line; x++){System.out.print(i++ + " ");}line++;System.out.println();}>
JaWarrior2a at 2007-7-9 22:47:20 > top of Java-index,Java Essentials,Java Programming...
# 7
Thank you for all your replies. It works good.
mathewronda at 2007-7-9 22:47:20 > top of Java-index,Java Essentials,Java Programming...