Table of Contents

Generate Pattern for a Number | Pictoblox

Example Description
Generating a pattern consists of rows starting from 1 and increasing by 1 with each row for a number input by the user.

Introduction:

This Python program takes a number input from the user and generates a pattern based on the given number. The pattern starts with 1 and increases by 1 with each row.

Code:

#Program to print the pattern for a number input by the user
#The output pattern to be generated is
#1
#1 2
#1 2 3
#1 2 3 4
#1 2 3 4 5
num = int(input("Enter a number to generate its pattern = "))
for i in range(1,num + 1):
 for j in range(1,i + 1):
  print(j, end = " ")
 print()

Logic:

The program uses nested for loops to print the pattern. The outer loop iterates from 1 to the given number. The inner loop iterates from 1 to the current value of the outer loop. In each iteration, the inner loop prints the value of the inner loop counter with a space, and at the end of each inner loop, a new line is printed.

Output:

The program generates the pattern based on the user’s input. The pattern consists of rows starting from 1 and increasing by 1 with each row. Each row contains the numbers from 1 to the current row number separated by spaces.

Enter a number to generate its pattern = 5

>> 1

>> 1 2

>> 1 2 3

>> 1 2 3 4

>> 1 2 3 4 5