Introduction
Number pattern programs are a great way to practice loops and control structures in Python. These programs are often asked in coding interviews to test your understanding of loops, nested loops, and conditional statements. Number patterns involve printing numbers in various shapes, such as triangles, pyramids, and diamonds. In this post, we will cover several popular number pattern programs in Python, along with detailed explanations and code.
1. Right-Angled Triangle Number Pattern
Description:
This pattern forms a right-angled triangle where the number of columns in each row increases by one, starting from 1.
Output:
1
12
123
1234
12345
Python Program:
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end="")
print()
Explanation:
- The outer loop controls the rows.
- The inner loop prints numbers from 1 to
i
, wherei
is the current row number.
2. Inverted Right-Angled Triangle Number Pattern
Description:
This pattern prints an inverted right-angled triangle where the number of columns (numbers) decreases with each row.
Output:
12345
1234
123
12
1
Python Program:
rows = 5
for i in range(rows, 0, -1):
for j in range(1, i + 1):
print(j, end="")
print()
Explanation:
- The outer loop starts from
rows
and decreases with each iteration. - The inner loop prints numbers from 1 to
i
for each row.
3. Pyramid Number Pattern
Description:
A pyramid pattern where numbers increase in each row, and the numbers are centered.
Output:
1
12
123
1234
12345
Python Program:
rows = 5
for i in range(1, rows + 1):
print(" " * (rows - i), end="")
for j in range(1, i + 1):
print(j, end="")
print()
Explanation:
- The first part prints spaces to center-align the numbers.
- The second part prints numbers from 1 to
i
for each row.
4. Inverted Pyramid Number Pattern
Description:
An inverted pyramid pattern where the numbers decrease in each row, and they are center-aligned.
Output:
12345
1234
123
12
1
Python Program:
rows = 5
for i in range(rows, 0, -1):
print(" " * (rows - i), end="")
for j in range(1, i + 1):
print(j, end="")
print()
Explanation:
- The first part prints leading spaces for alignment.
- The second part prints decreasing numbers as rows reduce.
5. Floyd’s Triangle
Description:
Floyd's Triangle is a right-angled triangle where the numbers are printed consecutively, starting from 1.
Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Python Program:
rows = 5
num = 1
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(num, end=" ")
num += 1
print()
Explanation:
- The outer loop controls the number of rows.
- The inner loop prints consecutive numbers, starting from 1.
6. Diamond Number Pattern
Description:
A diamond-shaped number pattern where numbers increase in the upper half and decrease in the lower half symmetrically.
Output:
1
121
12321
1234321
123454321
1234321
12321
121
1
Python Program:
rows = 5
# Upper part
for i in range(1, rows + 1):
print(" " * (rows - i), end="")
for j in range(1, i + 1):
print(j, end="")
for j in range(i - 1, 0, -1):
print(j, end="")
print()
# Lower part
for i in range(rows - 1, 0, -1):
print(" " * (rows - 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:
- The first part prints the upper half of the diamond.
- The second part prints the lower half of the diamond symmetrically.
7. Pascal’s Triangle
Description:
Pascal’s Triangle is a triangle where each number is the sum of the two numbers directly above it.
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Python Program:
rows = 5
for i in range(rows):
print(" " * (rows - i), end="")
num = 1
for j in range(i + 1):
print(num, end=" ")
num = num * (i - j) // (j + 1)
print()
Explanation:
- The outer loop controls the number of rows.
- The inner loop prints the numbers of Pascal's Triangle using the binomial coefficient formula.
8. Number Palindrome Pyramid
Description:
This pyramid pattern forms a palindromic sequence of numbers in each row.
Output:
1
121
12321
1234321
123454321
Python Program:
rows = 5
for i in range(1, rows + 1):
print(" " * (rows - 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:
- The first part prints spaces for alignment.
- The second and third parts print increasing and decreasing numbers, forming a palindrome.
9. Repeated Number Triangle Pattern
Description:
A triangle where each row contains repeated numbers equal to the row number.
Output:
1
22
333
4444
55555
Python Program:
rows = 5
for i in range(1, rows + 1):
print(str(i) * i)
Explanation:
- The outer loop controls the rows.
- Each row prints the row number repeated
i
times.
10. Hollow Square Number Pattern
Description:
A hollow square where the boundary is filled with numbers, and the inside is hollow.
Output:
11111
1 1
1 1
1 1
11111
Python Program:
rows = 5
for i in range(rows):
if i == 0 or i == rows - 1:
print("1" * rows)
else:
print("1" + " " * (rows - 2) + "1")
Explanation:
- The outer loop prints the first and last rows completely filled with numbers.
- The inner rows have numbers at the boundaries with spaces in between.
11. Alternating Binary Number Triangle
Description:
This pattern prints alternating binary numbers in a right-angled triangle.
Output:
1
01
101
0101
10101
Python Program:
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print((i + j) % 2, end="")
print()
Explanation:
- The outer loop controls the number of rows.
- The inner loop alternates between printing 1 and 0 based on the sum of
i
andj
.
12. Zigzag Number Pattern
Description:
This pattern forms a zigzag number pattern where numbers alternate between rows.
Output:
1
23
456
78910
Python Program:
rows = 4
num = 1
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(num, end="")
num += 1
print()
Explanation:
- The outer loop controls the rows.
- The inner loop prints consecutive numbers, forming a zigzag pattern.
13. Reverse Number Triangle
Description:
A reverse triangle where numbers decrease from the row number to 1 in each row.
Output:
1
21
321
4321
54321
Python Program:
rows = 5
for i in range(1, rows + 1):
for j
in range(i, 0, -1):
print(j, end="")
print()
Explanation:
- The outer loop controls the number of rows.
- The inner loop prints numbers in reverse from
i
to 1.
14. Square Number Pattern
Description:
A square pattern where each row contains the same number, equal to the row number.
Output:
11111
22222
33333
44444
55555
Python Program:
rows = 5
for i in range(1, rows + 1):
print(str(i) * rows)
Explanation:
- The outer loop controls the number of rows.
- Each row prints the row number repeated
rows
times.
15. Reversed Floyd’s Triangle
Description:
This pattern prints a reversed Floyd's triangle where numbers decrease as you move down the triangle.
Output:
5
54
543
5432
54321
Python Program:
rows = 5
for i in range(rows, 0, -1):
for j in range(rows, rows - i, -1):
print(j, end="")
print()
Explanation:
- The outer loop controls the number of rows.
- The inner loop prints numbers in reverse, starting from the current row and decreasing.
Conclusion
These 15 Python number pattern programs provide a variety of shapes, including triangles, pyramids, squares, and diamonds. By practicing these patterns, you can enhance your understanding of loops, nested loops, and conditional statements in Python. These patterns are frequently asked in coding interviews and are great for developing problem-solving skills.
Comments
Post a Comment
Leave Comment