📚Intermediate

Print Pascal's Pattern Triangle Pyramid

Print Pascal triangle

In Pascal's Triangle, each number is the sum of the two numbers directly above it. Row n, position k value = C(n,k) = n! / (k! × (n-k)!)

Program Code

pascals_triangle.c
C
1#include <stdio.h>
2
3int main() {
4 int rows;
5
6 printf("Enter number of rows: ");
7 scanf("%d", &rows);
8
9 for (int i = 0; i < rows; i++) {
10 // Print spaces for alignment
11 for (int j = 0; j < rows - i - 1; j++) {
12 printf(" ");
13 }
14
15 int num = 1;
16 for (int k = 0; k <= i; k++) {
17 printf("%4d", num);
18 num = num * (i - k) / (k + 1);
19 }
20 printf("\n");
21 }
22
23 return 0;
24}
Output

Enter number of rows: 5

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

Want to Learn More?

Explore our comprehensive tutorials for in-depth explanations of C programming concepts.

Browse Tutorials
Back to All Examples