Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i print this design pattern

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 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading