Pascal’s Triangle in C – Recurrence, Centered Output, and C(n,k) Formula

Pascal’s Triangle is a triangular array where each number is the sum of the two numbers directly above it. Row n, column k holds the binomial coefficient C(n,k) — the number of ways to choose k items from n. It appears in combinatorics, probability, the binomial theorem, and polynomial expansion. The C program below builds the triangle using the recurrence relation, prints it centered, and then demonstrates the closed-form C(n,k) formula. Both approaches are O(n²) in time and space.

The Recurrence and the Triangle

Row 0:              1
Row 1:            1   1
Row 2:          1   2   1
Row 3:        1   3   3   1
Row 4:      1   4   6   4   1
Row 5:    1   5  10  10   5   1
Row 6:  1   6  15  20  15   6   1

Rules:

  • Every row starts and ends with 1: tri[i][0] = tri[i][i] = 1
  • Every interior element is the sum of the two elements directly above it: tri[i][j] = tri[i-1][j-1] + tri[i-1][j]

This recurrence is the same as Pascal’s identity for binomial coefficients: C(n,k) = C(n−1,k−1) + C(n−1,k).

C Program: Pascal’s Triangle

/* Pascal's Triangle
 * Compile: gcc -ansi -Wall -Wextra pascal.c -o pascal */
#include <stdio.h>
#define MAXN 15

int main(void)
{
    int tri[MAXN][MAXN];
    int n, i, j;

    printf("Enter number of rows (1-%d): ", MAXN);
    if (scanf("%d", &n) != 1 || n < 1 || n > MAXN) {
        printf("Invalid.\n"); return 1;
    }

    /* Build using the recurrence */
    for (i = 0; i < n; i++) {
        tri[i][0] = 1;
        tri[i][i] = 1;
        for (j = 1; j < i; j++)
            tri[i][j] = tri[i - 1][j - 1] + tri[i - 1][j];
    }

    /* Print centered: each value in a 4-char field.
       Indent = 2*(n-1-i) keeps the triangle symmetric around a
       vertical axis. */
    for (i = 0; i < n; i++) {
        printf("%*s", 2 * (n - 1 - i), "");
        for (j = 0; j <= i; j++)
            printf("%4d", tri[i][j]);
        printf("\n");
    }
    return 0;
}

Sample Output (7 rows)

Enter number of rows (1-15): 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

Code Explanation

  • Build phase: The two boundary assignments (tri[i][0] = 1, tri[i][i] = 1) handle the edges of each row. The inner loop fills only the interior positions j = 1..i-1, which are the only positions where the recurrence applies. For row 0 and row 1 the inner loop body never executes (i < 1 and i < 2 respectively), so only the boundary assignments run — correct, since rows 0 and 1 are just [1] and [1,1].
  • Centering with %*s: The format specifier printf("%*s", width, "") prints width spaces by using the integer argument as the field width for an empty string. The indent 2*(n-1-i) decreases by 2 per row; since each row gains one 4-char element, the visual center stays fixed.
  • %4d per element: This gives each number a 4-character right-aligned field. For row n=15, the largest value is C(14,7) = 3432 (4 digits), which just fits. For n > 15, values exceed 4 digits and the alignment would break — increase the field width or reduce MAXN.
  • Memory: tri[MAXN][MAXN] allocates a 15×15 array (225 ints = 900 bytes) on the stack. Only the lower-triangular half is used, but the declaration is still clean and the wasted space is negligible.

The Closed-Form Formula: C(n,k) Without a Table

To compute a single value C(n,k) without building the whole triangle, use the multiplicative formula. It avoids factorial overflow by dividing at every step:

/* Compute C(n,k) using the multiplicative formula.
 * Compile: gcc -ansi -Wall -Wextra cnk.c -o cnk */
#include <stdio.h>

long long cnk(int n, int k)
{
    long long result = 1;
    int i;
    if (k > n - k) k = n - k;    /* C(n,k) == C(n,n-k); use smaller k */
    for (i = 0; i < k; i++) {
        result = result * (n - i) / (i + 1);
    }
    return result;
}

int main(void)
{
    int n, k;
    printf("Enter n and k: ");
    if (scanf("%d %d", &n, &k) != 2 || k < 0 || k > n) {
        printf("Invalid.\n"); return 1;
    }
    printf("C(%d,%d) = %lld\n", n, k, cnk(n, k));
    return 0;
}

Key Properties of Pascal’s Triangle

Property Formula Example
Row sum Sum of row n = 2ⁿ Row 4: 1+4+6+4+1 = 16 = 2⁴
Binomial expansion Coefficients of (1+x)ⁿ are row n (1+x)³ = 1·x⁰ + 3·x¹ + 3·x² + 1·x³
Triangular numbers Third diagonal: 1,3,6,10,15,… C(n,2) = n(n-1)/2
Hockey stick identity C(r,r)+C(r+1,r)+…+C(n,r) = C(n+1,r+1) 1+3+6+10 = C(5,2) = 10? No: = C(5,3)=10 ✓
Symmetry C(n,k) = C(n,n-k) C(6,2) = C(6,4) = 15
Prime rows All interior entries of prime row p are divisible by p Row 5: 10, 10 both divisible by 5

Frequently Asked Questions

How do you print just one row of Pascal’s Triangle in C without building the whole table?

Use the multiplicative formula: start with val=1 for the first element (C(n,0)=1). Each subsequent element is val = val * (n - j) / (j + 1) where j is the current column index (0-based). This computes each element from the previous one in O(1) using the identity C(n,j+1) = C(n,j) × (n-j)/(j+1). The division always yields an integer at each step.

What is the connection between Pascal’s Triangle and the Fibonacci sequence?

The Fibonacci sequence appears in Pascal’s Triangle by summing shallow diagonals. Summing C(0,0), then C(1,0), C(0,1)=no, then C(2,0), C(1,1) — going diagonally from bottom-left to top-right — gives 1,1,2,3,5,8,13,21… which is Fibonacci. Specifically, the n-th Fibonacci number equals the sum ∑ C(n-1-k, k) for k = 0,1,2,…

What is the largest value Pascal’s Triangle can hold in a C int?

A 32-bit int holds values up to about 2.1 billion. The central binomial coefficient C(34,17) ≈ 2.3 billion overflows. Safe limit: n ≤ 33 using int, or n ≤ 66 using long long (max ~9.2×10¹⁸, central coefficient C(66,33) ≈ 7.2×10¹⁸). The program above limits to MAXN=15, where the largest value is C(14,7)=3432, safely within int.

Related Programs

Recommended books:
The C Programming Language — K&R (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Test your C knowledge: C Aptitude Questions — or try our C Programming Quiz App on Android.

4 comments on “Pascal’s Triangle in C – Recurrence, Centered Output, and C(n,k) Formula

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>