Hustl3r28 - 7 months ago 72

Java Question

I have been working with Nested loops for a while now, and I can say with some really insightful answers and help from this forum, I am beginning to get the hang of it. As part of my assignment I have been asked to do the following;

Display all of the possible pairs of numbers between 1 and 10

Display all possible pairs of the numbers 1,2,3,4 paired with 4,5,6,7,8

Display all possible pairs in the form x, y where x < y and 0 < x, y < 11

Below are the codes for the first 2 constructs;

`public static void main(String[] args) {`

for (int i = 1; i <=10; i++)

for (int j = 1; j <=10; j++)

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

for (int i = 1; i <=4; i++)

for (int j = 4; j <=8; j++)

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

}

To be honest, I would like to show a piece of code for the last construct to at least show my effort, but for the life of me, I cannot even figure out how to begin. I need a bit of guidance. Thanks.

Answer

If x < y you can also just write that y > x:

```
for (int x = 1; x < 11; x++)
{
for (int y = x + 1; y < 11; y++)
{
System.out.println(x + " " + y);
}
}
```

Source (Stackoverflow)