C Practice Example 61 - Yang Hui Triangle

100 Classic C Examples

Title:Print out Pascal's triangle (requiring 10 rows to be printed).

Program analysis:

The structure is as follows:

1
1    1
1    2    1
1    3    3    1
1    4    6    4    1

Example

// Created by www.example.com on 15/11/9. // Copyright © 2015 Example. All rights reserved. // #include <stdio.h> int main() { int i,j; int a[10][10]; printf("\n"); for(i=0;i<10;i++) { a[i][0]=1; a[i][i]=1; } for(i=2;i<10;i++) for(j=1;j<i;j++) a[i][j]=a[i-1][j-1]+a[i-1][j]; for(i=0;i<10;i++) { for(j=0;j<=i;j++) printf("%5d",a[i][j]); printf("\n"); } }

The output result of the above code execution is:

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
1    8   28   56   70   56   28    8    1
1    9   36   84  126  126   84   36    9    1

100 Classic C Examples

other extensions