- To print sideway triangle pattern using numbers
User Input: 3
Output:
1 1 2 1 2 3 1 2 1
columns=int(input("Enter number of columns : "))
trows=2*columns-1
k=1
for i in range(1,trows+1):
num=1
for j in range(1,k+1):
print(num, end=" ")
num+=1
if(i<columns):
k+=1
else:
k-=1
print("")
Enter number of columns: 4 1 1 2 1 2 3 1 2 3 4 1 2 3 1 2 1Program
Main Logic :
trows=2*columns-1 # trows : total rows to print
k=1; # Used to trigger decreasing order of number
for i in range(1,trows+1):
num=1
for j in range(1,k+1):
print(num, end=" ")
num+=1
#check for position change at which triangle number value to decrease
if(i<columns):
k+=1
else:
k-=1
print("")
Concept