Write a program in python to print following patterns.
(a) 1
2 2
3 3 3
4 4 4 4
(b) 1
1 2
1 2 3
1 2 3 4
(c) 1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
Answers
Answer:
num=int(input("Enter number of row:"))
for i in range(1,num+1):
for j in range(1,num-i+1):
print(end=" ")
for j in range(i,0,-1):
print(j,end="")
for j in range(2,i+1):
print(j,end="")
print()
Explanation:
Simple pyramid pattern
# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern
def pypart(n):
# outer loop to handle number of rows
# n in this case
for i in range(0, n):
# inner loop to handle number of columns
# values changing acc. to outer loop
for j in range(0, i+1):
# printing stars
print("* ",end="")
# ending line after each row
print("\r")
# Driver Code
n = 5
pypart(n)
Output:
*
* *
* * *
* * * *
* * * * *