0% found this document useful (0 votes)
27 views13 pages

Pattern BASED PROBLEMS

The document contains a series of Python programming exercises focused on generating various numerical patterns, including triangles, pyramids, and squares. Each question provides a specific pattern to be printed, along with the corresponding Python code and example output. The patterns range from simple sequences to more complex shapes like Pascal's Triangle and diamond shapes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views13 pages

Pattern BASED PROBLEMS

The document contains a series of Python programming exercises focused on generating various numerical patterns, including triangles, pyramids, and squares. Each question provides a specific pattern to be printed, along with the corresponding Python code and example output. The patterns range from simple sequences to more complex shapes like Pascal's Triangle and diamond shapes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Questions Based on Patterns

Q.1. Write a Python Program to print the following


pattern:

2 2

3 3 3

4 4 4 4

Program:

n=int(input("enter the number: "))


for i in range(0,n+1):
for j in range(0,i):
print(i,end=" ")
print("\n")

Output:
enter the number: 4
1

2 2

3 3 3

4 4 4 4
Q.2. Write a Python Program to print the following
pattern:
Or
Write a Program to print Floyd's Triangle.
Example for n=5:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Program:

n=int(input("enter the number: "))


num=1
for i in range(0,n):
for j in range(0,i+1):
print(num,end=" ")
num=num+1
print()
output:
enter the number: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Q.3.Write a python Program to print a right-angled
triangle of numbers.
Example for n=5:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

# Right-angled Triangle
print("Right-angled Triangle:")
for i in range(1, n+1):
for j in range(1, i+1):
print(j, end=' ')
print()
print()

Q.4.Write a python program to print an inverted


half pyramid of numbers.
Example for n=5:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

# Inverted Half Pyramid


print("Inverted Half Pyramid:")
for i in range(n, 0, -1):
for j in range(1, i+1):
print(j, end=' ')
print()
print()
Q.4. Write a Python Program to print the following
pattern:
5 4 3 2 1

4 3 2 1

3 2 1

2 1

Program
n=int(input('enter the number: '))
for i in range(n,0,-1):
for j in range(i,0,-1):
print(j,end=" ")
print("\n")

Output:
enter the number: 5
5 4 3 2 1

4 3 2 1

3 2 1

2 1

1
Q.6.Write a python program to print a full pyramid
pattern of numbers.
Example for n=5:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Program:

n = 5
# Full Pyramid Pattern
print("Full Pyramid Pattern:")
for i in range(1, n+1):
# Print leading spaces
print(' ' * (n-i), end='')

# Print numbers
for j in range(1, i+1):
print(j, end=' ')
print()
print()

Q.7.Write a python program print a square pattern of


numbers.
Example for n=4:
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

# Number Square Pattern


print("Number Square Pattern:")
for i in range(1, n+1):
for j in range(1, n+1):
print(j, end=' ')
print()
print()
Q.8.Write a python program print Pascal's Triangle.
Example for n=5:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
# Pascal's Triangle
print("Pascal's Triangle:")
for i in range(n):
# Print leading spaces
print(' ' * (n-i), end='')

# Initialize value as 1 at the start of each row


val = 1
for j in range(i + 1):
print(val, end=' ')
# Calculate the next value for the row using binomial
coefficients
val = val * (i - j) // (j + 1)
print()
print()
Q.9.Write a Python program to print a zig-zag pattern of
numbers.
Example for n=5:
1 5
2 4
3
2 4
1 5

# Zig-Zag Pattern
print("Zig-Zag Pattern:")
for i in range(1, n+1):
for j in range(1, n+1):
if j == i or j == (n - i + 1):
print(j, end=' ')
else:
print(' ', end=' ')
print()
print()

Q.10.Write a Python program to print an X-pattern of


numbers.
Example for n=5:
1 1
2 2
3 3
3 3
2 2
1 1

# X-Pattern
print("X-Pattern:")
for i in range(1, n+1):
for j in range(1, n+1):
if i == j or j == (n - i + 1):
print(i, end=' ')
else:
print(' ', end=' ')
print()
print()
Q.11.Write a Python Program to print a diamond shape with
numbers.
Example for n=5:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
# Diamond Shape
print("Diamond Shape:")
# Upper part of the diamond
for i in range(1, n+1):
print(' ' * (n-i), end='')
for j in range(1, i+1):
print(j, end=' ')
print()

# Lower part of the diamond


for i in range(n-1, 0, -1):
print(' ' * (n-i), end='')
for j in range(1, i+1):
print(j, end=' ')
print()
print()
Q.12.Write a python program to print an hourglass pattern
with numbers.
Example for n=5:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
# Program for Hourglass Pattern
print("Hourglass Pattern:")
# Upper inverted part
for i in range(n, 0, -1):
print(' ' * (n-i), end='')
for j in range(1, i+1):
print(j, end=' ')
print()

# Lower half pyramid


for i in range(2, n+1):
print(' ' * (n-i), end='')
for j in range(1, i+1):
print(j, end=' ')
print()

Q.13.Write a Python Program of half-pyramid pattern using * shape.


# Number of rows
n = 5
# Loop for each row
for i in range(1, n + 1):
# Print '*' for each column in the row
print('*' * i)
Q.14.Write a Python Program to print hollow half pyramid as shown below:

Program
rows = 5 # Number of rows in the pyramid
for i in range(1, rows + 1):
for j in range(1, i + 1):
# Print stars on the edges or on the last row
if j == 1 or j == i or i == rows:
print("*", end=" ")
else:
print(" ", end=" ")
print() # Move to the next line

OutPut:
Q.15.Write a Python Program to print inverted hollow half pyramid as shown
below:

rows = 5 # Number of rows in the pyramid


for i in range(rows, 0, -1):
for j in range(1, i + 1):
# Print stars at the edges or on the top row
if j == 1 or j == i or i == rows:
print("*", end=" ")
else:
print(" ", end=" ")
print() # Move to the next line

Output:
Q.16.Write a Python Program to print hollow full pyramid as shown below:

rows = 5 # Number of rows in the pyramid


for i in range(1, rows + 1):
# Printing spaces
for j in range(rows - i):
print(" ", end="")

# Printing stars and spaces


for j in range(2 * i - 1):
if i == rows or j == 0 or j == 2 * i - 2:
print("*", end="")
else:
print(" ", end="")
print() # Move to the next line

Output
Q.17.Write a Python Program to print full pyramid as shown below:

rows = 5 # Number of rows in the pyramid


for i in range(1, rows + 1):
# Printing leading spaces
for j in range(rows - i):
print(" ", end=" ")
# Printing numbers and spaces
for j in range(1, 2 * i) :
if j == 1 or j == 2 * i - 1: # Edges of the row
print(j // 2 + 1, end=" ")
elif i == rows and j % 2 == 1: # Last row without
repetition

print(j // 2 + 1, end=" ")


else:
print(" ", end=" ") # Space inside the
pyramid
print() # Move to the next line

Output:

You might also like