Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print a numerical triangle [duplicate]

Tags:

python

You are given a positive integer (example: N = 6). Print a numerical triangle of height like the one below:

1
22
333
4444
55555 

Use no more than two lines of code. Use a for loop and print function only. You can't use anything related to strings. N is an input.

What I tried:

for i in range(1,int(input())):
    print(*range(1, i+1))

my output:

1
1 2
1 2 3
1 2 3 4
like image 340
purple geminii Avatar asked May 09 '26 03:05

purple geminii


1 Answers

One way to get the triangle indicated (without using strings) is to treat the output as numbers and to calculate those numbers:

N = 6
for i in range(1, N):
    print(sum(10**j for j in range(i)) * i)

Output as requested.

like image 70
quamrana Avatar answered May 11 '26 17:05

quamrana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!