Write a C program to generate the given pascal triangle.
4
3 4 3
2 3 4 3 2
1 2 3 4 3 2 1
4
3 4 3
2 3 4 3 2
1 2 3 4 3 2 1
int main() {
int n, i , j, data;
printf("Enter the no of line:");
scanf("%d", &n);
for (i = n; i > 0; i--) {
data = i;
/* space at the front of triangle */
for (j = i; j > 0; j--) {
printf(" ");
}
/* print values of first half of triangle */
for (j = n; j >= i; j--) {
printf("%3d", data++);
}
data = data - 2;
/* print values of second half of triangle */
for (j = n; j > i; j--) {
printf("%3d", data--);
}
printf("\n");
}
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter the no of line:5
5
4 5 4
3 4 5 4 3
2 3 4 5 4 3 2
1 2 3 4 5 4 3 2 1
Enter the no of line:5
5
4 5 4
3 4 5 4 3
2 3 4 5 4 3 2
1 2 3 4 5 4 3 2 1
No comments:
Post a Comment