I am currently trying to code below pattern … but not able to think how the logic need to be defined for it. It is first time where i am not having any clue how to start with it
Expected output :
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
>Solution :
You could use a nested loop. I’ve explained the variable meaning in the comment in the code
# Define the number of rows for the pattern
#in your question it's 5, but I will test with 6
num_rows = 6
# Create a variable to keep track of the current number (in each row)
current_num = 1
# Loop over each row
for i in range(num_rows):
# Loop over each number (column) in the current row
for j in range(i + 1):
# Print the current number and a space
print(current_num, end=" ")
# Increment the current number
current_num += 1
# Go to the next line after each row (new line)
print()
# 1
# 2 3
# 4 5 6
# 7 8 9 10
# 11 12 13 14 15
# 16 17 18 19 20 21