- How to print Pascal's triangle using nested for loops
- How to calculate binomial coefficients using
C(n, r) - How to print leading spaces to shape the triangle
- How to calculate factorial using a loop
- How to avoid using arrays for this pattern
- Why Pascal's triangle is useful in combinations and algebra
How Pascal's Triangle Works
Pascal's triangle is a triangular arrangement of numbers in which each number is the sum of the two numbers directly above it. The first and last number in every row is always 1.
Each number in Pascal's triangle represents a binomial coefficient. The value at row n and position r is calculated using the formula \(C(n, r) = \frac{n!}{r!(n-r)!}\).
To print the triangle, the program uses nested for loops. The outer loop controls the rows, the first inner loop prints spaces for alignment, and the second inner loop calculates and prints the numbers in each row.
💡 In one line: Print spaces for alignment, calculate each value using the binomial coefficient formula, and display every row using nested for loops.
The Three Steps of Printing the Triangle
| Step | Loop operation | Effect |
|---|---|---|
| 1. Print spaces | for (space = 1; space <= rows - i; space++) | Creates the triangular shape by shifting each row to the right |
| 2. Calculate coefficients | C(n, r) = n! / (r! * (n-r)!) | Generates the numbers for the current row |
| 3. Print the row | for (r = 0; r <= i; r++) | Displays all binomial coefficients in the current row |
The first few rows of Pascal's triangle are:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
In row 4, the middle value 6 is obtained by adding the two numbers above it: 3 + 3 = 6. This addition property is the main idea behind Pascal's triangle.
Complete C Program
Program: print Pascal's triangle using nested for loops
#include <stdio.h>
/* Function to calculate factorial */
long factorial(int number) {
long result = 1;
int i;
for (i = 1; i <= number; i++) {
result = result * i;
}
return result;
}
/* Function to calculate binomial coefficient C(n, r) */
long binomial_coefficient(int n, int r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
int main(void) {
int rows, i, j, space;
printf("Enter the number of rows: ");
if (scanf("%d", &rows) != 1 || rows <= 0) {
printf("Invalid input. Please enter a positive integer.\n");
return 1;
}
for (i = 0; i < rows; i++) {
/* Print leading spaces */
for (space = 1; space <= rows - i; space++) {
printf(" ");
}
/* Print binomial coefficients */
for (j = 0; j <= i; j++) {
printf("%4ld ", binomial_coefficient(i, j));
}
printf("\n");
}
return 0;
}
Program Output
Enter the number of rows: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
A second run, with 7 rows:
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
The program prints the required number of rows. Each row begins and ends with 1, and every middle value is the sum of the two values above it.
Step-by-Step Explanation
Let us understand how the program prints Pascal's triangle for 5 rows.
1. The factorial function
factorial(int number) calculates the factorial of a number using a for loop. For example, factorial(4) returns \(4 \times 3 \times 2 \times 1 = 24\).
2. The binomial coefficient function
binomial_coefficient(n, r) calculates \(C(n, r)\) using the formula \(n! / (r! \times (n-r)!)\). This formula gives the value at row n and position r in Pascal's triangle.
3. Outer loop for rows
The outer loop for (i = 0; i < rows; i++) controls the rows. When i = 0, the program prints the first row. When i = 4, it prints the fifth row.
4. Inner loop for spaces
The first inner loop prints spaces before the numbers. The number of spaces decreases as the row number increases, which creates the triangular shape.
5. Inner loop for numbers
The second inner loop prints values from position 0 to position i. For row 4, it prints five values: 1, 4, 6, 4, and 1.
6. Example calculation for row 4
C(4, 0) = 1C(4, 1) = 4C(4, 2) = 6C(4, 3) = 4C(4, 4) = 1
Therefore, row 4 is printed as 1 4 6 4 1.
7. Why nested loops are needed
One loop is needed to move through the rows, another loop is needed to print spaces, and another loop is needed to print the values in each row. This is why the program uses nested for loops.
8. Why the first and last values are 1
In the formula \(C(n, r)\), when r = 0 or r = n, the result is always 1. Therefore, every row of Pascal's triangle starts and ends with 1.
9. Importance of Pascal's triangle
Pascal's triangle is used in algebra, probability, and combinatorics. Its values represent the number of ways to choose r items from n items, also written as nCr.
10. Complexity summary
The time complexity is high because the program calculates factorials repeatedly for every binomial coefficient. For n rows, the factorial-based approach takes approximately O(n3) time in the worst case. Space complexity is O(1) because no array is used to store the triangle.
🎯 Remember this: Pascal's triangle is made of binomial coefficients. Use \(C(n, r) = \frac{n!}{r!(n-r)!}\), print spaces for alignment, and use nested for loops to display each row.
Frequently Asked Questions
1. What is Pascal's triangle?
Pascal's triangle is a triangular arrangement of numbers where each number is the sum of the two numbers directly above it. Every row begins and ends with 1.
2. Why are binomial coefficients used?
Each value in Pascal's triangle is a binomial coefficient. The value at row n and position r is \(C(n, r)\), which represents the number of ways to choose r items from n items.
3. Why are nested for loops required?
Nested loops are required because the program must handle rows, spaces, and values. The outer loop controls rows, while the inner loops print spaces and calculate the numbers for each row.
4. Can Pascal's triangle be printed without factorial?
Yes. A more efficient method uses the relationship \(C(n, r+1) = C(n, r) \times (n-r) / (r+1)\). This avoids calculating factorials repeatedly.
5. Why does each row start and end with 1?
In the binomial coefficient formula, \(C(n, 0)\) and \(C(n, n)\) are always 1. Therefore, the first and last numbers of every row are 1.
6. What is the time complexity of this program?
The factorial-based version has approximately O(n3) time complexity because factorials are recalculated for every coefficient. Space complexity is O(1) because the program does not store the triangle in an array.
💡 Tip: After implementing this program, try printing Pascal's triangle without using the factorial function. Use the previous coefficient to calculate the next coefficient in the same row.