Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #PASCAL TRIANGLE
- no = int(input("Enter the number of rows: "))
- n = 1 #variable n representes the current row
- while n <= no: #loop checking for each row
- totalterms = n + 1 #total terms in a row
- term = 0 # current term (or also known as r)
- line = "" #the row in string form
- n_num = 1 #n!
- n_r = 1 #(n-r)!
- r = 1 #r!
- while term < totalterms: #calculating coefficient of each term in a row consecutively through a loop
- j = 1 #j, u, and p are variables for calculating factorials
- for j in range(1, n + 1): #n! calculation loop
- n_num *= j
- u = 1
- for u in range(1, (n - term) + 1): #(n-r)! calculation loop
- n_r *= u
- p = 1
- for p in range(1, term + 1): #r! calculation loop
- r *= p
- line = line + str(n_num//(n_r * r)) + " " #adding the calculated coefficient to the row one after another
- term += 1
- n_num = 1 #resetting the n! value for the next calculation
- n_r = 1 #resetting the (n-r)! value for the next calculation
- r = 1 #resetting the r! value for the next calculation
- #s = n - no
- s = 0
- while s < (no - n): #loop to add spaces to each row so that they all form a triangle
- line = " " + line
- s += 1
- print(line) #print the row finally
- n += 1
Add Comment
Please, Sign In to add comment