on Apr 29 2022 Comment

#include <stdio.h>

// Function to calculate the factorial of a number
int factorial(int n) {
    if (n == 0 || n == 1) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

// Function to calculate the combination of two numbers
int combination(int n, int r) {
    return factorial(n) / (factorial(r) * factorial(n - r));
}

// Function to print Pascal's Triangle
void printPascalsTriangle(int rows) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j <= i; j++) {
            printf("%d ", combination(i, j));
        }
        printf("\n");
    }
}

// Main function
int main() {
    int rows;

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

    // Check if the input is valid
    if (rows <= 0) {
        printf("Invalid input. Number of rows should be greater than 0.\n");
        return 1;
    }

    // Print Pascal's Triangle
    printPascalsTriangle(rows);

    return 0;
}