Pascal’s Triangle is a triangular array of numbers where each number is the sum of the two numbers directly above it. It has applications in combinatorics, algebra, and probability. This program demonstrates how to print Pascal’s Triangle using nested loops and combinatorial calculations in C.
Understanding The Problem
The program should take the number of rows as input and print Pascal’s Triangle. Each number in the triangle is a combination C(n, k)
calculated as n! / (k! * (n-k)!)
. Proper formatting is necessary to maintain the triangular shape.
Steps to Solve the Problem:
- Take the number of rows from the user.
- Use nested loops to control rows and positions.
- Calculate the value of each element using factorial or a running formula.
- Print spaces for alignment to form a triangle.
- Move to a new line after printing each row.
Solution 1: Using Factorial Function
The factorial method calculates each combination using the formula C(n, k) = n! / (k! * (n-k)!)
.
#include <stdio.h>
// Function to calculate factorial
long long factorial(int n) {
long long fact = 1;
for(int i = 1; i <= n; i++)
fact *= i;
return fact;
}
// Function to calculate combination
long long combination(int n, int k) {
return factorial(n) / (factorial(k) * factorial(n - k));
}
int main() {
int rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for(int i = 0; i < rows; i++) {
// Print leading spaces
for(int j = 0; j < rows - i - 1; j++) {
printf(" ");
}
// Print combination values
for(int k = 0; k <= i; k++) {
printf("%lld ", combination(i, k));
}
printf("\n");
}
return 0;
}
In this program, the outer loop controls the rows. The first inner loop prints spaces to align numbers in a triangle. The second inner loop prints the values of C(n, k)
for each position in the row, creating Pascal’s Triangle.

Solution 2: Using Running Formula
A more efficient method avoids repeated factorial calculations by using the property C(n, k+1) = C(n, k) * (n-k) / (k+1)
.
#include <stdio.h>
int main() {
int rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for(int i = 0; i < rows; i++) {
long long value = 1;
// Print leading spaces
for(int j = 0; j < rows - i - 1; j++)
printf(" ");
for(int k = 0; k <= i; k++) {
printf("%lld ", value);
value = value * (i - k) / (k + 1); // Update for next element
}
printf("\n");
}
return 0;
}
This method is more efficient for large rows because it calculates each element iteratively without using factorials, reducing computational overhead.

FAQs
Q1: Can Pascal’s Triangle be printed for very large rows?
Yes, but using the running formula method is more efficient to avoid factorial overflow.
Q2: Can the triangle be inverted or hollow?
Yes, formatting can be adjusted to create inverted or hollow Pascal triangles for visualization purposes.
Q3: What data type should be used for large numbers?
For larger rows, long long
or arbitrary-precision libraries are recommended to avoid integer overflow.
Conclusion
This program demonstrates how to print Pascal’s Triangle using factorial calculations or an iterative running formula. Proper formatting ensures a triangular structure, and these methods can be adapted for larger rows or for creating variations like inverted triangles.
References & Additional Resources
- C Programming Tutorial – Covers loops, functions, and combinatorial calculations in C.
- GeeksforGeeks – Pattern Programs in C – Examples and explanations for printing Pascal’s Triangle in C.
- TutorialsPoint – C Loops – Explanation of nested loops and formatting in C.