Pascal's Triangle is a form of a triangular array of the binomial coefficient. You might have read about pascal triangle in Binomial theorem chapter in the maths. We will some of the concepts of binomial in this program. In this post, we will see the generation mechanism of the pascal triangle or how the pascals triangle is generated, understanding the pascal's Triangle in c with the algorithm of pascals triangle in c, the program of pascal's Triangle in c.

Here we will see the learn the simplest method of writing a c program for the pascal's Triangle. We will first take input from the user for no of rows of pascal triangle.

To understand the following programme(pascal's Triangle), you must have knowledge of the following elements in C programming topics:

  • Loop (for loop)
  • Comments in c
  • Automatic operators in c
  • Functions ( print() and scanf() )
  • escape sequence in c ( /n, /t )

Program for Pascal's Triangle in C language


pascal-triangle-c

Generally, In the pascal's Triangle, each number is the sum of the top row nearby number and the value of the edge will always be one. You can see in the figure given above. As you can see, it forms a system of numbers arranged in rows forming a triangle. Each row consists of the coefficients in the expansion of  (a + b)^n where n will be 1, 2, 3, 4 and so on.

The Triangle starts with one at the top row, each row starts with one and end with one. Each new number is added below the top one. And the number obtained is just the sum of the two numbers above. Except the for the starting and ending.

Here's how the row actually obtained

1st Row   = 1
2nd Row  = (0 + 1), (1 + 0) = 1, 1
3rd Row   = (0 + 1), ( 1 + 1), ( 1+ 0) = 1, 2, 1
4th Row   = (0 + 1), (1 + 2), (2 + 1), (1 + 0) = 1, 3 , 3, 1
5th Row   = (0 + 1), (1 + 3), (3 + 3), (3 + 1), (1 + 0) =1, 4, 6, 4, 1

As you can see the sum of all numbers of a row element is the twice the sum of the no of the previous row. In the third row, 1+1 is 2 and, the fourth row, 1+2 and 2+1 are 3, in the fifth row, 1+3, 3+3, 3+1 that is 4,6,4 respectively.

Algorithm of pascal's Triangle in C

A simple algorithm of the pascal's Triangle in c/c++. An algorithm improves the rate of execution of the problem. First, try to understand the algorithm before going through the programme.

1. Start
2. Declare the variables i, j, n, c, m
3. Enter the no of row wanted in the pascal triangle
4. Initialize m → n-1
5. Repeat the process from step 6 to 9
6. Do the following operations in the loop until i <  n
7. Initialize c → 1
8. Print space
9. Assassin C → C * (i - j) / j
10. Print the final required Triangle
11. Stop

Following is the program for the pascal triangle in C

Go through with the comments to properly understand the pascal's Triangle in c.


#include <stdio.h>
int main() {
// Program to print the pascal triangle
//Coded by S-P
             
    int i,j,n,c,m;                                                                    // consider variables
    printf("Enter the no of rows for pascal triangle \n");
    scanf("%d",&n);                                                            // input from user
    m=n-1;
    for(i=1;i<=n;i++)                     
    {
        c=1;
        for(j=1;j<=m;j++)
        {
            printf(" ");
        }
        for(j=1;j<=i;j++)                                                        //  LOGIC
        {
            printf("%d",c);
            printf (" ");                                                             // Space between the numbers
            c=c*(i-j)/j;                                                           
        }
        printf("\n");
        m--;
    }
}

Output:

Enter the no of rows for pascal triangle

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

Download the file of pascal's Triangle here

download-button


If you find this post interesting and helpful, do share with friends who are facing this problem and follow our website spyronation so that you get more stuff like this and you will get notified when we will post something. If you have any issues related to this post, please feel free to ask we will try to look into your problem. If you have any problem doing this method and not able to understand then comment below, we will try to solve it.                                                          

1 Comments

  1. Respected Sir, the logic you used is nice, but for me I think I will define a function name Permutation and define their how a Permutation works and then I can use that function in the main loop as row no. C flag. Flag goes from 0 to no of characters. Basically I am a blogger and do wordPress blogging. I’m new to programming but how would you do this for random letters on a keyboard. I want to make a simple triangle with 3 different letters or numbers. E.g = A,S,D

    ReplyDelete

Post a Comment

Previous Post Next Post