Welcome folks today in this blog post we will be
printing pascal's triangle pattern upto n rows in python using recursion.All the full source code of the application is given below.
In order to get started you need to make an
app.py file and copy paste the following code
import sys # Recursive method to create the mathematical series def pascal(col,row): if(col == 0) or (col == row): return 1 else: return pascal(col-1,row-1) + pascal(col,row-1) # Method returns the results of n rows in the triangle def PascalTriangle(num): if (num <= 0): print('Number must be greater than zero') for r in range(num): for c in range(r+1): sys.stdout.write(str(pascal(c,r))+' ') sys.stdout.write('\n') #Test Section PascalTriangle(10)
Now if you execute the
python script by typing the below command as shown below