Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Exercise 61 - Pascal's Triangle

C Exercise 61 - Pascal's Triangle

100 Classic C Language Examples 100 Classic C Language Examples

**Problem:** Print Pascal's triangle (required to print 10 rows).

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.runoob.com on 15/11/9.

// Copyright (c) 2015 Runoob Tutorial. 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 of the above code 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 Language Examples 100 Classic C Language Examples