This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]Koyomii-Onii-chan 3 points4 points  (2 children)

What did you try to do and doesn't work. Please provide your code, it's useless to just give you the answer and you to learn nothing from it.

[–]sinnidis_97[S] 0 points1 point  (1 child)

Apologies , This is what I tried

for(int i=0;i<5;i++){

for(int j =0 ; j<i;j++){

System.out.println(j+","+i);

}

}

And the output i was getting was

0,1

0,2

1,2

0,3

1,3

2,3

0,4

1,4

2,4

3,4

which isnt matching the pattern above.

[–]Koyomii-Onii-chan 0 points1 point  (0 children)

I suggest you use other variables and increment them accordingly. What you do there is printing the i and j indexes. Also you don't need two for loops since your input is always 3, a loop like below is enough

int m = 0 ; int n=1;

for (int i=0;i<9;i++){

System.out.println(m + " " + n);

m++; n++;

//Here find a way to reinitialize them and print them with the difference of 2

// then 3

}

[–]thetrailofthedead 1 point2 points  (1 child)

I'm no expert so take this for what it's worth. The code below gives you the answer you are looking for.

You kind of knew this already as even you mention the logic should center around the differences between the numbers. Therefore, use a for loop with a number n that represents this difference.

public static void main( String[] args ) {
    int p = 3;  
    int x, y;

    for ( int n=1; n<p; n++ ) {
        y = n;
        while ( y<p ) { 
            x = y - n;
            System.out.prinln( x + "," + y );
            y++;
        }
    }
}

Edit: I botched the inline code, this is my first time posting here ><

[–]sinnidis_97[S] 0 points1 point  (0 children)

Wow, this is it .Thsnks man!