Posted on by Kalkicode
Code Pattern

Program for pascals triangle in python

Pascal triangle

Python Program for pascals triangle. Here mentioned other language solution.

#  Python 3 program for printing Pascal's triangle patterns

#  Printing Pascal's triangle patterns by number of rows
def pascalTriangle( row) :
    #  Loop controlling variables
    i = 0
    j = 0
    k = 0
    #  Loop from 0..n
    i = 0
    while (i < row) :
        #  Print initial white space
        j = i + 1
        while (j < row) :
            #  include space
            print("\t", end ="")
            j += 1
        #  Print resultant value
        j = 0
        while (j <= i) :
            if (j == 0 or i == 0) :
                #  Set initial values
                k = 1
            else :
                #  Pascal's triangle number calculation
                k = int(k * (i - j + 1) / j)
            #  Display the value of calculate number
            print("\t" + str(k) + "\t", end ="")
            j += 1
        #  Include new line
        print()
        i += 1
    #  Include new line
    print()


if __name__=="__main__":
    #  Test
    #  When Row : 6
    pascalTriangle(6)
    #  When Row : 10
    pascalTriangle(10)

Output

						1
					1		1
				1		2		1
			1		3		3		1
		1		4		6		4		1
	1		5		10		10		5		1

										1
									1		1
								1		2		1
							1		3		3		1
						1		4		6		4		1
					1		5		10		10		5		1
				1		6		15		20		15		6		1
			1		7		21		35		35		21		7		1
		1		8		28		56		70		56		28		8		1
	1		9		36		84		126		126		84		36		9		1

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment