Pattern Printing & Problem Solving Exercises
Pattern printing exercises help develop logical thinking and problem-solving skills. They teach you how to break down complex problems into smaller, manageable steps.
These exercises improve your understanding of loops, conditional statements, and algorithmic thinking - essential skills for any programmer!
Try to solve these pattern printing problems. Solutions are provided below each problem.
Print a right-angled triangle pattern with 5 rows:
rows = 5
for i in range(1, rows + 1):
print('*' * i)
Print an inverted right-angled triangle pattern with 5 rows:
rows = 5
for i in range(rows, 0, -1):
print('*' * i)
Print a pyramid pattern with 4 rows:
rows = 4
for i in range(1, rows + 1):
spaces = ' ' * (rows - i)
stars = '*' * (2 * i - 1)
print(spaces + stars)
Print a number triangle with 5 rows:
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end='')
print()
Print a diamond pattern with 5 rows in the top half:
n = 5
# Top half
for i in range(1, n+1, 2):
spaces = ' ' * ((n - i) // 2)
print(spaces + '*' * i)
# Bottom half
for i in range(n-2, 0, -2):
spaces = ' ' * ((n - i) // 2)
print(spaces + '*' * i)
Print a hollow square pattern with side length 5:
size = 5
for i in range(size):
for j in range(size):
if i == 0 or i == size-1 or j == 0 or j == size-1:
print('*', end='')
else:
print(' ', end='')
print()
Print an alphabet pyramid with 5 rows:
rows = 5
for i in range(1, rows + 1):
for j in range(i):
print(chr(65 + j), end='')
print()
Print a number pyramid with 5 rows:
rows = 5
for i in range(1, rows + 1):
print(str(i) * i)
Print Floyd's Triangle with 5 rows:
rows = 5
number = 1
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(number, end=' ')
number += 1
print()
Print the first 5 rows of Pascal's Triangle:
rows = 5
for i in range(rows):
# Print leading spaces
print(' ' * (rows - i), end='')
# Calculate and print values
value = 1
for j in range(i + 1):
print(value, end=' ')
value = value * (i - j) // (j + 1)
print()
Print a cross pattern with size 5:
size = 5
for i in range(size):
for j in range(size):
if i == j or i + j == size - 1:
print('*', end='')
else:
print(' ', end='')
print()
Print a binary number triangle with 5 rows:
rows = 5
for i in range(1, rows + 1):
start = 1 if i % 2 == 1 else 0
for j in range(i):
print(start, end='')
start = 1 - start # Flip between 0 and 1
print()
Print a hollow diamond pattern with 5 rows in the top half:
n = 5
# Top half
for i in range(1, n+1):
spaces = ' ' * (n - i)
if i == 1:
print(spaces + '*')
else:
print(spaces + '*' + ' ' * (2*i-3) + '*')
# Bottom half
for i in range(n-1, 0, -1):
spaces = ' ' * (n - i)
if i == 1:
print(spaces + '*')
else:
print(spaces + '*' + ' ' * (2*i-3) + '*')
Print a spiral number pattern with size 4:
n = 4
matrix = [[0]*n for _ in range(n)]
directions = [(0,1), (1,0), (0,-1), (-1,0)]
x, y, d = 0, 0, 0
for i in range(1, n*n+1):
matrix[x][y] = i
nx, ny = x + directions[d][0], y + directions[d][1]
if 0 <= nx < n and 0 <= ny < n and matrix[nx][ny] == 0:
x, y = nx, ny
else:
d = (d + 1) % 4
x, y = x + directions[d][0], y + directions[d][1]
for row in matrix:
for num in row:
print(f"{num:2}", end=' ')
print()
Print a zigzag pattern with height 4 and width 8:
height = 4
width = 8
for i in range(height):
line = [' '] * width
pos = i
step = 2 * (height - 1)
while pos < width:
line[pos] = '*'
pos += step
if step != 2 * (height - 1):
step = 2 * (height - 1) - step
print(''.join(line))