C Program to Print Pascal’s Triangle Pattern

Introduction

Pascal’s Triangle is a triangular array of binomial coefficients. The pattern starts with a 1 at the top, and each subsequent row is constructed by adding the two numbers directly above it. This exercise demonstrates how to generate Pascal’s Triangle using loops and calculations in C.

Problem Statement

Create a C program that:

  • Accepts the number of rows for Pascal's Triangle.
  • Prints Pascal's Triangle in a triangular format.

Example:

  • Input: rows = 5
  • Output:
        1
       1 1
      1 2 1
     1 3 3 1
    1 4 6 4 1
    

Solution Steps

  1. Input the Number of Rows: The size determines how many rows Pascal’s Triangle will have.
  2. Use Nested Loops: The outer loop handles the rows, and the inner loop calculates and prints the elements of Pascal’s Triangle.
  3. Calculate Pascal's Values: Use the formula C(n, k) = n! / (k! * (n - k)!) to compute the binomial coefficients.
  4. Display Pascal's Triangle: Print the values in a triangular arrangement.

C Program

#include <stdio.h>

// Function to calculate factorial
long long factorial(int num) {
    long long fact = 1;
    for (int i = 1; i <= num; i++) {
        fact *= i;
    }
    return fact;
}

// Function to calculate binomial coefficient
long long binomialCoefficient(int n, int k) {
    return factorial(n) / (factorial(k) * factorial(n - k));
}

int main() {
    int rows, i, j;

    // Step 1: Accept the number of rows for Pascal's Triangle
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    // Step 2: Outer loop for each row
    for (i = 0; i < rows; i++) {
        // Step 3: Print spaces for alignment
        for (j = 0; j < rows - i - 1; j++) {
            printf(" ");
        }

        // Step 4: Print the binomial coefficients for each row
        for (j = 0; j <= i; j++) {
            printf("%lld ", binomialCoefficient(i, j));
        }

        // Move to the next line after printing each row
        printf("\n");
    }

    return 0;
}

Explanation

Step 1: Input Number of Rows

  • The program first asks the user for the number of rows to print in Pascal’s Triangle.

Step 2: Outer Loop for Rows

  • The outer loop controls the number of rows printed, running from 0 to rows - 1.

Step 3: Print Spaces for Alignment

  • Spaces are printed to align the triangle properly so that the numbers form a pyramid shape.

Step 4: Calculate and Print Binomial Coefficients

  • The binomial coefficient is calculated using the formula C(n, k) = n! / (k! * (n - k)!).
  • The function binomialCoefficient() is used to calculate and print the values for each row.

Factorial Function

  • The factorial() function computes the factorial of a number, which is used to calculate the binomial coefficients.

Output Example

For rows = 5, the output will be:

    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1

For rows = 6, the output will be:

     1
    1 1
   1 2 1
  1 3 3 1
 1 4 6 4 1
1 5 10 10 5 1

Conclusion

This C program prints Pascal’s Triangle by calculating the binomial coefficients using nested loops and the factorial function. The program aligns the output in a triangular format, and this exercise is helpful for practicing loops and mathematical functions in C.

Comments