Computer Science
Write the output of the following:
for x in range(1, 6):
for y in range(1, x+1):
print (x, ' ', y)
Answer
1 1
2 1
2 2
3 1
3 2
3 3
4 1
4 2
4 3
4 4
5 1
5 2
5 3
5 4
5 5
Working
The code features two nested for
loops. The outer loop iterates over values from 1 to 5 (inclusive) and assigns each to x
. The inner loop iterates from 1 to the x + 1
value and assigns each to y
. The print(x, ' ', y)
statement outputs x
and y
separated by a space during each iteration. This arrangement generates combinations of (x, y) pairs.
Related Questions
Write the output of the following:
for i in [100, 200, 300]: print(i)
Write the output of the following:
for j in range(10, 6, -2): print(j*2)
Write the output of the following:
for x in range(10, 20): if (x == 15): break print(x)
Write the output of the following:
for x in range(10, 20): if (x % 2 == 0): continue print(x)