Pattern BASED PROBLEMS
Pattern BASED PROBLEMS
2 2
3 3 3
4 4 4 4
Program:
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:
# 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()
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()
# 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()
# 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()
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:
Output:
Q.16.Write a Python Program to print hollow full pyramid as shown below:
Output
Q.17.Write a Python Program to print full pyramid as shown below:
Output: