Pascal Triangle in C Program using For loop of Numbers
ADVERTISEMENTS
Pascal triangle in C program using for loop of numbers. In this article, you will learn how to print the pascal triangle in c program using for loop of numbers.
Source Code
// Pascal Triangle in C Program using For loop of Numbers
#include <stdio.h>
int main() {
int r, co = 1, ws, i, j;
// r - To store the number of rows
// co - To store the coefficient value
// ws - To store the white space
printf("-----Enter the number of rows to print the pattern-----\n");
scanf("%d", &r);
printf("\n-----This is the pascal triangle pattern-----\n\n");
for (i = 0; i < r; i++) {
printf("\t");
for (ws = 1; ws <= r - i; ws++)
printf(" ");
for (j = 0; j <= i; j++) {
if (j == 0 || i == 0)
co = 1;
else
co = co * (i - j + 1) / j;
printf("%4d", co);
}
printf("\n");
}
return 0;
}
Output
-----Enter the number of rows to print the pattern-----
8
-----This is the pascal triangle pattern-----
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
1 7 21 35 35 21 7 1