Print Pascal's Triangle Pattern in C / C++ / Java / Python / C#

Pascal's Triangle pattern

Programs to print the Pascal's triangle pattern have been given here. Each row in the pattern represents an array of binomial coefficients.






1. Program & output to print the pascal's triangle pattern




1.1. C Program & output to print the pascal's triangle pattern

Code has been copied
/***************************************
	  alphabetacoder.com
C program to print the Pascal's triangle
****************************************/

#include <stdio.h>

int main() {
    // declare variables
    int row, i, j, num;

    // take input
    printf("Enter the number of rows: ");
    scanf("%d", & row);

    // new line
    printf("\n");

    // display the pattern
    for (i = 1; i <= row; i++) {
        // initialize first coefficient
        num = 1;

        // print spaces
        for (j = 1; j <= row - i; j++)
            printf(" ");

        // print numbers
        for (j = 1; j <= i; j++) {
            printf("%d ", num);
            // calculate the next coefficient
            num = num * (i - j) / j;
        }
        // new line
        printf("\n");
    }

    return 0;
}

Output


Enter the number of rows: 6


     1 

    1 1 

   1 2 1 

  1 3 3 1 

 1 4 6 4 1 

1 5 10 10 5 1 




1.2. C++ Program & output to print the pascal's triangle pattern

Code has been copied
/*****************************************
	  alphabetacoder.com
C++ program to print the Pascal's triangle
******************************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int row, i, j, num;

    // take input
    cout << "Enter the number of rows: ";
    cin >> row;

    // new line
    cout << endl;

    // display the pattern
    for (i = 1; i <= row; i++) {
        // initialize first coefficient
        num = 1;

        // print spaces
        for (j = 1; j <= row - i; j++)
            cout << " ";

        // print numbers
        for (j = 1; j <= i; j++) {
            cout << num << " ";
            // calculate the next coefficient
            num = num * (i - j) / j;
        }
        // new line
        cout << endl;
    }

    return 0;
}

Output


Enter the number of rows: 7


      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.3. Java Program & output to print the pascal's triangle pattern

Code has been copied
/*******************************************
	     alphabetacoder.com
Java program to print the Pascal's triangle
********************************************/

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);
        // declare variables
        int row, i, j, num = 1;

        // take input 
        System.out.print("Enter the number of rows: ");
        row = sc.nextInt();

        // new line
        System.out.println("");

        // display the pattern
        for (i = 1; i <= row; i++) {
            // initialize first coefficient
            num = 1;

            // print spaces
            for (j = 1; j <= row - i; j++)
                System.out.print(" ");

            // print numbers
            for (j = 1; j <= i; j++) {
                System.out.print(num + " ");
                // calculate the next coefficient
                num = num * (i - j) / j;
            }
            // new line
            System.out.println("");
        }
    }
}

Output


Enter the number of rows: 5


    1 

   1 1 

  1 2 1 

 1 3 3 1 

1 4 6 4 1 




1.4. Python Program & output to print the pascal's triangle pattern

Code has been copied
#**********************************************
#	    alphabetacoder.com
#Python program to print the Pascal's triangle
#**********************************************


# take input
row = int(input("Enter the number of rows: "))

# new line
print("")

# display the pattern
for i in range(1, row + 1):
    # initialize first coefficient
    num = 1

    # print spaces
    for j in range(1, row - i + 1):
        print(" ", end = "")

    # print numbers
    for j in range(1, i + 1):
        print(num, end = " ")
        # calculate the next coefficient
        num = num * (i - j) // j
        
    # new line
    print("")

Output


Enter the number of rows: 4


   1 

  1 1 

 1 2 1 

1 3 3 1 




1.5. C# Program & output to print the pascal's triangle pattern

Code has been copied
/****************************************
	    alphabetacoder.com
C# program to print the Pascal's triangle
*****************************************/

using System;

namespace PascalTriangle {
    class Program {
        static void Main(string[] args) {
            // declare variables
            int row, i, j, num = 1;

            // take input
            Console.Write("Enter the number of rows: ");
            row = Convert.ToInt32(Console.ReadLine());

            // new line
            Console.WriteLine("");

            // display the pattern
            for (i = 1; i <= row; i++) {
                // initialize first coefficient
                num = 1;

                // print spaces
                for (j = 1; j <= row - i; j++)
                    Console.Write(" ");

                // print numbers
                for (j = 1; j <= i; j++) {
                    Console.Write(num + " ");
                    // calculate the next coefficient
                    num = num * (i - j) / j;
                }
                // new line
                Console.WriteLine("");
            }

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter the number of rows: 3


  1 

 1 1 

1 2 1