{"id":6946,"date":"2021-09-30T10:30:55","date_gmt":"2021-09-30T05:00:55","guid":{"rendered":"https:\/\/python-programs.com\/?p=6946"},"modified":"2021-11-22T18:35:33","modified_gmt":"2021-11-22T13:05:33","slug":"python-program-to-print-pascals-triangle","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-pascals-triangle\/","title":{"rendered":"Python program to print Pascal’s Triangle"},"content":{"rendered":"
Want to excel in java coding? Practice with these Java Programs examples with output<\/a> and write any kind of easy or difficult programs in the java language<\/p>\n Pascal Triangle Definition:<\/strong><\/p>\n Pascal’s triangle is a lovely shape produced by arranging numbers. The sum of the two numbers above it is used to produce each number. This triangle’s outside edges are always 1. The triangle is depicted in the diagram below.<\/p>\n <\/p>\n Explanation:<\/strong><\/p>\n To illustrate the triangle in a nutshell, the first line is 1. There are two ones in the line after that. The second line is here.<\/p>\n 1 2 1 is the third line, which is created by adding the ones from the previous line. Similarly, the number of 1 and 2 in an alternative pattern forms the fourth line, and so on.<\/p>\n Examples of pascal Triangle<\/strong><\/p>\n Example 1:<\/strong><\/p>\n Input :<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 1:<\/strong><\/p>\n Input :<\/strong><\/p>\n Output :<\/strong><\/p>\n Below are the methods to print the pascal triangle.<\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n Using the oneval variable, we will first initialise the top row in this function. Variable zerovalue=0 is also set to zero. To run the code for n iterations, we’ll use a for loop.<\/p>\n We’ll print the list generated by the oneval variable within the for loop. We’ll now add the onevalue to left and right elements. We’ve also used the zip feature in this case.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The following definition of a Binomial Coefficient may be used to optimise the above code the i’th entry in a line number line is Binomial Coefficient C(line, i) and all lines begin with value 1. The idea is to use C to measure C(line, i) using C(line, i-1).<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language Pascal Triangle Definition: Pascal’s triangle is a lovely shape produced by arranging numbers. The sum of the two numbers above it is used to produce each number. This …<\/p>\nheight =5<\/pre>\n
1\r\n 1 1\r\n 1 2 1\r\n 1 3 3 1\r\n 1 4 6 4 1<\/pre>\n
height =10<\/pre>\n
1\r\n 1 1\r\n 1 2 1\r\n 1 3 3 1\r\n 1 4 6 4 1\r\n 1 5 10 10 5 1\r\n 1 6 15 20 15 6 1\r\n 1 7 21 35 35 21 7 1\r\n 1 8 28 56 70 56 28 8 1\r\n 1 9 36 84 126 126 84 36 9 1<\/pre>\n
Pascal Triangle in Python<\/h2>\n
\n
Method #1: Implementation Using zip function<\/h3>\n
def printPascal(height):\r\n onevalue = [1]\r\n zero = [0]\r\n for i in range(height):\r\n print(*onevalue)\r\n onevalue = [left+right for left,\r\n right in zip(onevalue+zero, zero+onevalue)]\r\n\r\n\r\n# given height\r\nheight = 5\r\n# passing the height as paramter to printPascal function\r\nprintPascal(height)\r\n<\/pre>\n
1\r\n1 1\r\n1 2 1\r\n1 3 3 1\r\n1 4 6 4 1<\/pre>\n
Method #2:Using NCR<\/h3>\n
\n
# impoerting factorial fucntion from math module\r\nfrom math import factorial\r\n\r\n\r\ndef printPascal(height):\r\n for i in range(height):\r\n for j in range(height-i+1):\r\n\r\n # for spacing in left\r\n print(end=\" \")\r\n\r\n for j in range(i+1):\r\n print(factorial(i)\/\/(factorial(j)*factorial(i-j)), end=\" \")\r\n\r\n # printing in new line\r\n print()\r\n\r\n\r\n# given height\r\nheight = 5\r\n# passing the height as paramter to printPascal function\r\nprintPascal(height)\r\n<\/pre>\n
1 \r\n 1 1 \r\n 1 2 1 \r\n 1 3 3 1 \r\n 1 4 6 4 1<\/pre>\n
Method #3:Using Binomial Coefficients<\/h3>\n
C(line, i) = C(line, i-1) * (line - i + 1) \/ i<\/pre>\n
def printPascal(height):\r\n for i in range(1, height+1):\r\n for j in range(0, height-i+1):\r\n print(' ', end='')\r\n\r\n # since first eelement is always 1\r\n onevalue = 1\r\n for j in range(1, i+1):\r\n\r\n # first value of every line is 1 always.\r\n print(' ', onevalue, sep='', end='')\r\n\r\n # using binomial cofficients to calculat using onevalue\r\n onevalue = onevalue * (i - j) \/\/ j\r\n print()\r\n\r\n\r\n# given height\r\nheight = 5\r\n# passing the height as paramter to printPascal function\r\nprintPascal(height)\r\n<\/pre>\n
1\r\n 1 1\r\n 1 2 1\r\n 1 3 3 1\r\n 1 4 6 4 1<\/pre>\n
\n