101k views
5 votes
Which pattern is produced by the following code? for (int i = 1; i <= 6; i++) { for (int j = 6; j >= 1; j--) System.out.print(j <= i ? j + " " : " " + " "); System.out.println(); } Pattern A Pattern B Pattern C Pattern D 1 1 2 3 4 5 6 1 1 2 3 4 5 6 1 2 1 2 3 4 5 2 1 1 2 3 4 5 1 2 3 1 2 3 4 3 2 1 1 2 3 4 1 2 3 4 1 2 3 4 3 2 1 1 2 3 1 2 3 4 5 1 2 5 4 3 2 1 1 2 1 2 3 4 5 6 1 6 5 4 3 2 1 1

A. Pattern A
B. Pattern B
C. Pattern C
D. Pattern D

User Alphaaa
by
6.4k points

1 Answer

1 vote

Answer:

1

2 1

3 2 1

4 3 2 1

5 4 3 2 1

6 5 4 3 2 1

Step-by-step explanation:

I do not now know which option it corresponds but the shape should look like above.

The logic is following;

There is a nested for loop. The inner loop prints the value of j, when j is smaller than or equal to i. Otherwise, it prints a space.

For example, in the first iteration i = 1 and j starts from 6.

i = 1, j = 6 -> print space

i = 1, j = 5 -> print space

i = 1, j = 4 -> print space

i = 1, j = 3 -> print space

i = 1, j = 2 -> print space

i = 1, j = 1 -> print j

User Daniel Cooke
by
6.2k points