How do i print this design pattern

Question:

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 
Asked By: kolaton277

||

Answers:

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 

You can use a for loop to achieve this. Here is a simple function that does what you want; it takes a single argument of how many rows to print, so for example, if you set ‘rows’ to 5, it will print 5 rows, which is what you showed in your post.

Code:

def printpattern(rows):
    num = 1
    for i in range(0, rows):
        for j in range(0, i+1):
            print(num, end=' ')
            num = num + 1
        print()
Answered By: Cletus

This is called Floyd’s Triangle, if you want to research it. It’s a simple iteration for loop.

rows = int(input("num of rows: "))
num = 1

for i in range(1, rows+1):
    for j in range(1, i+1):
        print(num, end=' ')
        number += 1
    print()
Answered By: myrus earthwalker
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.