C Program for Pascal’s Triangle


The program code for printing Pascal’s Triangle is a very famous problems in C language. In this post, I have presented 2 different source codes in C program for Pascal’s triangle, one utilizing function and the other without using function. Both of these program codes generate Pascal’s Triangle as per the number of row entered by the user. And, to help to understand the source codes better, I have briefly explained each of them, plus included the output screen as well.

Although the peculiar pattern of this triangle was studied centuries ago in India, Iran, Italy, Greece, Germany and China, in much of the western world, Pascal’s triangle has been named after the French mathematician and physicist Blaise Pascal.

The construction of the triangular array in Pascal’s triangle is related to the binomial coefficients by Pascal’s rule. So, the basic trick, or rather the working principle of this program for Pascal’s triangle in C is based on binomial expansion and combination theorems of algebra.

How to Build Pascal’s Triangle?

C Program for Pascal's Triangle
The first thing one needs to know about Pascal’s triangle is that all the numbers outside the triangle are “0”s. To build the triangle, start with a “1” at the top, the continue putting numbers below in a triangular pattern so as to form a triangular array. So, each new number added below the top “1” is just the sum of the two numbers above, except for the edge which are all “1”s. This can be summarized as:

0 row =1
1 row = (0+1), (1+0) = 1, 1
2 row = (0+1), (1+1), (1+0) = 1, 2, 1
3 row = (0+1), (1+2), (2+1), (1+0) = 1, 3, 3, 1
4 row = (0+1), (1+3), (3+3), (3+1), (1+0) = 1, 4, 6, 4, 1

Properties of Pascal’s Triangle:

C Program for Pascal's Triangle Source Code

  • The sum of all the elements of a row is twice the sum of all the elements of its preceding row. For example, sum of second row  is 1+1= 2, and that of first is 1. Again, the sum of third row  is 1+2+1 =4, and that of second row is 1+1 =2, and so on. This major property is utilized to write the code in C program for Pascal’s triangle.
  • The sequence of the product of each element is related to the base of the natural logarithm, e.
  • The left and the right edges of Pascal’s triangle are filled with “1”s only.
  • All the numbers outside the triangle are “0”s.
  • The diagonals next to the edge diagonals contain natural numbers (1, 2, 3, 4, ….) in order.
  • The sum of the squares of the numbers of row “n” equals the middle number of row “2n”.

Source Code for Pascal’s Triangle in C:

The source code below uses a user defined function, long fun(int y) which is for the calculation of factorial whenever the function is called. All  variables (x,y and z) used in this program are of integer  data type. Out these three variables, x and z are for loop control and y has been defined to store the number of rows entered by the user. Compile it in Code::Blocks.

C Program for Pascal's Triangle

Pascal’s Triangle in C Without Using Function:

Using a function is the best method for printing Pascal’s triangle in C as it uses the concept of binomial coefficient. But, this alternative source code below involves no user defined function. Rather it involves a number of loops to print Pascal’s triangle in standard format. Here I will briefly describe the programming technique to help you understand the source code better. The output screen is same as in the previous case – with using function.

The source code of the program consists of six integer type of variable, named x, y, n, a, z and s. Out of these variable x,y and z have been defined to control the for() loop, the integer ‘n’ stores the limit of Pascal’s triangle entered by the user and the variable ‘s’ is for printing the space in triangle.

As the C program for Pascal’s triangle is executed, it first asks for the value of limit of the triangle. The program assigns s with n, i.e., number of space with the limit of Pascal’s triangle. Then, the variable “a” is initialized as a=1 within the for() loop in which “x” is the loop control variable. Again, in order to control the space, a nested for() loop with “z” as a control variable is used.

Finally, for printing the elements in this program for Pascal’s triangle in C, another nested for() loop of control variable “y” has been used. The formula used to generate the numbers of Pascal’s triangle is: a=(a*(x-y)/(y+1).

After printing one complete row of numbers of Pascal’s triangle, the control comes out of the nested loops and goes to next line as commanded by \n code. The process repeats till the control number specified is reached. To terminate the program, any character can be entered due to use of getch() function at the end of source code.

Also see,
Pascal’s Triangle Algorithm/Flowchart

The above source codes are totally error-free, and have been coded perfectly to eradicate bugs. If you have any other alternative C source codes related to Pascal’s triangle, share them with us. Also, your queries, suggestions and feedback regarding this C program can be brought to us from the comments section below.


  1. Thank you verry much
    That help me a lot.
    U cn do it without usinfg ‘s’ variable.
    instead u cn use
    This condition.
    N I was having one question,
    How do u get that formula to generate nos.

  2. Good!
    Two quick point, I want to point out
    a=(a*(x-y)/(y+1) should be a=(a*(x-y)/(y+1))
    you missed the closing bracket.
    In the second program the condition of first loop should be x<n not x<=n
    because the index starts from 0.


Please enter your comment!
Please enter your name here