Tuesday, 30 June 2020

Print Triangle pattern in python

Triangle Number Pattern


Problem Description: You are given with an input number N, then you have to print the given star pattern corresponding to that number N.
For example if N=4 Pattern output : 1
22
333
4444
How to approach?
1. Take N as input from the user.
2. Figure out the number of rows, (which is N here) and run a loop for that.
3. Now, figure out how many columns are to be printed in ith row and run a loop for that within this.
4. Now, figure out “What to print?” in a (row, column) number. It can depend on the column number, row number or N.
Pseudo code for the given problem:
input=N i=1
While i is less than or equal to N:
j=1
While j is less than or equal to i:
print(i) Increment j by 1
Increment i by 1 Add a new line here
❏ Let us dry run the Code for N=4
● i=1(<=4)
➔ j=1(<=1), so print ”1”
➔ j=2 (>1), move out of the inner loop with a new line
● i=2(<=4)
➔ j=1 (<=2), so print “2”
➔ j=2 (<=2), so print “2”
➔ j=3(>2), move out of the inner loop with a new line
● i=3(<=4)
➔ j=1(<=3), so print “3”
➔ j=2(<=3), so print “3”
➔ j=3(<=3), so print “3”
➔ j=4(>3), move out of the inner loop with a new line
● i=4(<=4)
➔ j=1(<=4), so print “4”
➔ j=2(<=4), so print “4”
➔ j=3(<=4), so print “4”
➔ j=4(<=4), so print “4”
➔ j=5(>4), move out of the inner loop with a new line
● i=5(>4), move out of the loop So , final output:
1
22
333
4444

CODE:
## Read input as specified in the question
## Print the required output in given format
n = int(input())
i = 1
while i <= n:
    j = 1
    while j <= i: 
        print(i, end = '')
        j = j+1
    print()
    i = i + 1

Sample Input:
5

Sample Output :
1
22
333
4444
55555


No comments:

Post a Comment

Print Triangle pattern in python

Triangle Number Pattern Problem Description : You are given with an input number N, then you have to print the given star pattern corres...