How to print the triangle pattern of numbers 1 to 15 in python?
Question:
This image is my question While printing the triangle pattern from 1 to 15 numbers in 5 number of rows. I have gone through some error.
Answers:
If you want to print 1 to 21 then
Next = 1
for s in range(0, 6):
for t in range(0, s + 1):
print(Next, end=' ')
Next= Next + 1
print()
Output :
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
If you want to print 1 to 15 then
Next = 1
for s in range(0, 5):
for t in range(0, s + 1):
print(Next, end=' ')
Next= Next + 1
print()
Output :
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Hope this code works for you. Solution with O[N] complexity.
col = 0
curr = 1
N = 15
for i in range(1,N+1):
if curr <col:
curr+=1
else:
print()
col+=1
curr=1
print(i,end=' ')
This image is my question While printing the triangle pattern from 1 to 15 numbers in 5 number of rows. I have gone through some error.
If you want to print 1 to 21 then
Next = 1
for s in range(0, 6):
for t in range(0, s + 1):
print(Next, end=' ')
Next= Next + 1
print()
Output :
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
If you want to print 1 to 15 then
Next = 1
for s in range(0, 5):
for t in range(0, s + 1):
print(Next, end=' ')
Next= Next + 1
print()
Output :
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Hope this code works for you. Solution with O[N] complexity.
col = 0
curr = 1
N = 15
for i in range(1,N+1):
if curr <col:
curr+=1
else:
print()
col+=1
curr=1
print(i,end=' ')