15 Pattern Printing Questions with Solutions and Explanations.
Pattern printing is a great way to improve logical thinking and problem-solving skills. Below are 15 unique pattern questions, their solutions, and step-by-step explanations.
1️⃣ Right-Angled Triangle Pattern
Problem Statement:
Print the following pattern for N = 5:
*
**
***
****
*****
Solution:
n = 5 for i in range(1, n+1): print('*' *
i)
Explanation:
- Loop runs from 1 to N.
'*' * i
printsi
stars in each row.
2️⃣ Inverted Right-Angled Triangle
Problem Statement:
Print the following pattern for N = 5:
*****
****
***
**
*
Solution:
n = 5
for i in range(n, 0, -1):
print('*' * i)
Explanation:
- Loop runs in reverse order from N to 1.
- Each row has
i
stars.
3️⃣ Pyramid Pattern
Problem Statement:
Print the following pyramid pattern for N = 5:
*
***
*****
*******
*********
Solution:
n = 5
for i in range(n):
print(' ' * (n-i-1) + '*' * (2*i+1))
Explanation:
- Spaces decrease while stars increase in each row.
2*i+1
ensures odd numbers of stars per row.
4️⃣ Diamond Pattern
Problem Statement:
Print the following diamond pattern for N = 5:
*
***
*****
*******
*********
*******
*****
***
*
Solution:
def print_diamond(n):
for i in range(n):
print(' ' * (n-i-1) + '*' * (2*i+1))
for i in range(n-2, -1, -1):
print(' ' * (n-i-1) + '*' * (2*i+1))
print_diamond(5)
Explanation:
- First loop prints a pyramid.
- Second loop prints an inverted pyramid.
5️⃣ Hollow Square Pattern
Problem Statement:
Print a hollow square for N = 5:
*****
* *
* *
* *
*****
Solution:
n = 5
for i in range(n):
if i == 0 or i == n-1:
print('*' * n)
else:
print('*' + ' ' * (n-2) + '*')
Explanation:
- First and last row print
N
stars. - Middle rows print stars at both ends.
6️⃣ Zig-Zag Pattern
Problem Statement:
Print a Zig-Zag pattern for N = 5:
* * *
* * * * *
* * *
Solution:
n = 5
for i in range(3):
for j in range(n):
if (i + j) % 4 == 0 or (i == 1 and j % 4 == 2):
print('*', end=' ')
else:
print(' ', end=' ')
print()
Explanation:
- Conditions are used to place stars at specific locations.
7️⃣ Hollow Triangle
Problem Statement:
Print the following hollow triangle pattern for N = 5:
*
* *
* *
* *
*********
Solution:
n = 5
for i in range(n):
spaces = ' ' * (n - i - 1)
if i == 0:
stars = '*'
elif i == n - 1:
stars = '*' * (2 * i + 1)
else:
stars = '*' + ' ' * (2 * i - 1) + '*'
print(spaces + stars)
Eplanation:
- The first row has a single star.
- The last row is filled completely.
- Intermediate rows have spaces in between.
8️⃣ Number Pyramid
Problem Statement:
Print a number pyramid for N = 5:
1
121
12321
1234321
123454321
Solution:
n = 5
for i in range(1, n+1):
print(' ' * (n-i), end='')
for j in range(1, i+1):
print(j, end='')
for j in range(i-1, 0, -1):
print(j, end='')
print()
Explanation:
- Each row prints numbers increasing, then decreasing.
- Spaces are used for alignment.
9️⃣ Hollow Diamond Pattern
Problem Statement:
Print the following hollow diamond pattern for N = 5:
*
* *
* *
* *
*********
* *
* *
* *
*
Solution:
n = 5
# Upper part of the pattern
for i in range(n):
spaces = ' ' * (n - i - 1)
if i == 0:
print(spaces + '*')
else:
inner_spaces = ' ' * (2 * i - 1)
print(spaces + '*' + inner_spaces + '*')
# Lower part of the pattern
for i in range(n - 2, -1, -1):
spaces = ' ' * (n - i - 1)
if i == 0:
print(spaces + '*')
else:
inner_spaces = ' ' * (2 * i - 1)
print(spaces + '*' + inner_spaces + '*')
Explanation:
- Top half follows increasing spaces with two stars on the edges.
- The middle row is fully filled.
- The bottom half mirrors the top half.
🔟 Butterfly Pattern
Problem Statement:
Print the following butterfly pattern for N = 4:
* *
** **
*** ***
********
*** ***
** **
* *
Solution:
n = 4
# Upper part of the pattern
for i in range(1, n + 1):
left_stars = '*' * i
spaces = ' ' * (2 * (n - i))
right_stars = '*' * i
print(left_stars + spaces + right_stars)
# Lower part of the pattern
for i in range(n - 1, 0, -1):
left_stars = '*' * i
spaces = ' ' * (2 * (n - i))
right_stars = '*' * i
print(left_stars + spaces + right_stars)
Explanation:
- The first half prints increasing stars, spaces, and then stars again.
- The second half is the reverse of the first half.
1️⃣1️⃣ Pascal’s Triangle
Problem Statement:
Print Pascal’s Triangle for N = 5:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Solution:
def pascal(n):
for i in range(n):
num = 1
print(' ' * (n - i), end='')
for j in range(i + 1):
print(num, end=' ')
num = num * (i - j) // (j + 1)
print()
pascal(5)
Explanation:
- Uses binomial coefficients to calculate values.
- The formula
num = num * (i - j) // (j + 1)
computes the next number in the row.
1️⃣2️⃣ X Pattern
Problem Statement:
Print an X pattern for N = 5:
* *
* *
*
* *
* *
Solution:
n = 5
for i in range(n):
for j in range(n):
if i == j or i + j == n - 1:
print('*', end='')
else:
print(' ', end='')
print()
Explanation:
- Stars appear where
i == j
(diagonal) ori + j == n - 1
(anti-diagonal).
1️⃣3️⃣ Hourglass Pattern
Problem Statement:
Print the following hourglass pattern for N = 5:
*********
* *
* *
* *
*
* *
* *
* *
*********
Solution:
n = 5
# Upper part of the pattern
for i in range(n):
leading_spaces = ' ' * i
middle_spaces = ' ' * (2 * (n - i - 1) - 1)
right_star = '*' if i != n - 1 else ''
print(leading_spaces + '*' + middle_spaces + right_star)
# Lower part of the pattern
for i in range(n - 2, -1, -1):
leading_spaces = ' ' * i
middle_spaces = ' ' * (2 * (n - i - 1) - 1)
right_star = '*' if i != n - 1 else ''
print(leading_spaces + '*' + middle_spaces + right_star)
Explanation:
- The pattern is similar to a diamond, but inverted.
1️⃣4️⃣ Palindromic Number Pyramid
Problem Statement:
Print the palindromic number pyramid for N = 5:
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
Solution:
n = 5
for i in range(1, n + 1):
print(' ' * (n - i), end='')
for j in range(i, 0, -1):
print(j, end=' ')
for j in range(2, i + 1):
print(j, end=' ')
print()
Explanation:
- First, numbers decrease from
i
to1
. - Then, numbers increase from
2
toi
. - Spaces are used to align the pyramid.
1️⃣5️⃣ Hollow Pyramid Pattern
Problem Statement:
Print the following hollow pyramid pattern for N = 5:
*
* *
* *
* *
*********
Solution:
n = 5
for i in range(n):
leading_spaces = ' ' * (n - i - 1)
middle_spaces = ' ' * (2 * i - 1)
right_star = '*' if i > 0 else ''
if i == n - 1:
print('*' * (2 * i + 1))
else:
print(leading_spaces + '*' + middle_spaces + right_star)
Explanation:
- Top rows have stars at the edges and spaces in between.
- Last row is completely filled with stars