Write a program to print the Pascal triangle.
Pascal Triangle:
1
1 1
1 2 1
1 3 3 1
C Program
#include <stdio.h>int main(){int rows;int val;printf("Enter number of rows: ");scanf("%d", &rows);for (int i = 0; i < rows; i++){//print the spacesfor (int space = 1; space <= rows - i; space++){printf(" ");}//print the valuesfor (int j = 0; j <= i; j++){//if first and lastif (j == 0 || j == i){val = 1;}else{val = val * (i - j + 1) / j;}printf("%4d ", val);}printf("\n");}return 0;}
Output:
Enter number of rows: 5 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
No comments:
Post a Comment