Questions

What is Pascal Triangle in Python?

Pascals Triangle in python is a number pattern in the shape of a triangle.

python triangle

Each number in the triangle is the sum of the two numbers above it. There are many ways to implement the pascals triangle in python. One of which is by using the nCr formula.

n!=n!/(n-r)!r!

python triangle

For example:
                    

# Python program to print pascal's triangle

from math import factorial

# input value of n

n = 5

#uncomment this line to take input from user

#n=int(input("Enter the value of n: "))

for i in range(n):

    for j in range(n-i+1):

        # for spacing

        print(end=" ")

    for j in range(i+1):

        # nCr formula      

print(factorial(i)//(factorial(j)*factorial(i-j)), end=" ")

    # for new line

print()

Output:

      

                    

      1
 
     1 1

   1 2 1

  1 3 3 1

 1 4 6 4 1

Related Questions

Related Topics

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.