codescracker
c

C Program to Print Pascal's Triangle



« Previous Program Next Program »

In this article, you will learn and get code on printing of Pascal's triangle in C programming. But before going through the program, if you are not aware of Pascal's triangle, then I recommend you to refer the short description on Pascal's Triangle. There, in very short time, you will get everything that is required to create a program on it. But for now, the following picture tells everything about it.

pascal triangle c

This figure defines Pascal's triangle. But I've written the simplest algorithm to create a Pascal's triangle in that article.

Print Pascal's Triangle

Let's create a program to print Pascal's triangle without using any function and formula. This program only follows the algorithm to expand Pascal's triangle using loops and logic.

#include<stdio.h>
#include<conio.h>
int main()
{
    int row, col, i=1, j=0, arr[5], arrTemp[5];
    arr[0] = 1;
    arr[1] = 1;
    for(row=0; row<5; row++)
    {
        for(col=4; col>row; col--)
            printf(" ");
        for(col=0; col<=row; col++)
        {
            if(row==0)
                printf("1");
            else
            {
                if(col==0 || col==row)
                    printf("1 ");
                else
                {
                    arrTemp[i] = arr[j]+arr[j+1];
                    printf("%d ", arrTemp[i]);
                    i++;
                    j++;
                }
            }
        }
        printf("\n");
        arrTemp[i] = 1;
        if(row>1)
        {
            j=0;
            arr[j]=1;
            for(j=1, i=1; j<=row; j++, i++)
                arr[j] = arrTemp[i];
            i=1;
            j=0;
        }
    }
    getch();
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample output.

c program print pascal triangle

In above program, we have used two arrays in a way that the first array say arr[] holds the column's value of previous row, whereas the second array say arrTemp[] holds the column's value of next row. That is,:

The dry run of above program goes like:

  1. Initially, i=1, j=0, arr[0]=1, arr[1]=1
  2. Now using for loop, 0 initialized to row (now row=0)
  3. Checks whether it is less than 5 or not. The condition evaluates to be true, therefore program flow goes inside the loop. Inside it, using the first for loop, 4 spaces gets printed
  4. Now program flow goes to second for loop
  5. There 0 is initialized to col (now col=0)
  6. Checks whether col is less than or equal to the value of row or not
  7. The condition evaluates to be true, therefore program flow goes inside this loop. Using if statement, checks whether the value of row is equal to 0 or not
  8. The condition evaluates to be true, therefore program flow goes inside if block and prints 1 on output. The else block gets skipped
  9. Now program flow goes to update part of inner first for loop, increments the value of col
  10. Now col=1
  11. Process step no.6 with new value of col
  12. The condition evaluates to be false
  13. Now the next statement is
    printf("\n");
    that tells the compiler to start next output things from next or new line
  14. Now 1 is initialized to arrTemp[i]. Because previous value of i was 1. Therefore arrTemp[1] = 1
  15. An if block is created to check whether the value of row is greater than 1 or not
  16. The condition evaluates to be false, therefore if block gets skipped.
  17. And program flow goes to the update part of outer for loop. The value of row gets incremented
  18. Now row=1
  19. Process step no.3 to 5
  20. Process step no.6 with new value of row (that is 1)
  21. Now col=0 again
  22. Read step no.7
  23. The condition evaluates to be false, because row is not equal to 0
  24. Therefore program flow goes to else block. Inside the else block, checks whether the value of col is equal to 0 or row's value or not
  25. Because it's value is equal to 0, therefore 1 gets printed
  26. Increments the value of col (now col=1)
  27. And checks whether it is less than or equal to row or not. Condition evaluates to be true, program flow again goes inside the loop
  28. Again else block gets executed
  29. And again inside it, the condition of if block evaluates to be true, because col (1) equals row (1). Therefore 1 gets printed again
  30. Again increments the value of col (now col=2) and checks the condition. This time, condition evaluates to be false
  31. Process step no.13 to 17
  32. Now row=2
  33. Process step no.3 to 5
  34. Process step no.6 with new value of row (that is 2)
  35. Now col=0 again
  36. Process step no.7, 23, 24, 25, 26, 27, 28
  37. Now col=2
  38. This time, the condition of if block evaluates to be false, therefore program flow goes to else block
  39. There, arrTemp[i] = arr[j]+arr[j+1] or arrTemp[1] = arr[0]+arr[0+1] or arrTemp[1] = 1+1 or arrTemp[1] = 2
  40. Now i=2 and j=1
  41. Read step no.9
  42. Now col=3
  43. Process step no.6 with new value of col
  44. Process step no.12 to 15
  45. The condition evaluates to be true, therefore program flow goes inside the if block
  46. Now j=0, arr[j]=1 or arr[0]=1
  47. The for loop, gets executed. After successfully executing it
  48. We will have, arr[0]=1, arr[1]=2, arr[2]=1
  49. Now i=1 and j=0
  50. Process step no.17
  51. Now row=3
  52. Process continue from step no.33 until the value of row equals 5. That is the condition of outer for loop evaluates to be false

Print Pascal's Triangle using Formula

Now let's create another program that does the same job, but uses a formula to find the column value (one by one) and gets printed directly without so much logic as given in above program. But to improve programming skills, better to approach with previous one.

#include<stdio.h>
#include<conio.h>
long int fact(int);
int main()
{
    int i, c;
    for(i=0; i<5; i++)
    {
        for(c=4; c>i; c--)
            printf(" ");
        for(c=0; c<=i; c++)
            printf("%ld ", fact(i)/(fact(c)*fact(i-c)));
        printf("\n");
    }
    getch();
    return 0;
}
long int fact(int n)
{
    int i, res=1;
    for(i=1; i<=n; i++)
        res = res*i;
    return res;
}

It will produce the same output as of previous one. The formula is given in the separate tutorial on Pascal's triangle. That is, the column value of every row of a Pascal's triangle can be calculated as:

value = (row!)/((column!)*(row-columns)!)

where row is the row number, and col is the column number.

Note - Row and column both starts from 0.

The ! represents factorial. To understand, how to find factorial of a number, then refer to find factorial in C.

For example, the value at 2nd column of 4th row will be:

value = (row!)/((column!)*(row-columns)!)
      = (4!)/((2!)*(4-2)!)
      = (24)/(2*(2!))
      = 24/(2*2)
      = 24/4
      = 6

So 6 is the number present at fourth row and second column, that is actually at fifth row and third column.

Print Pascal's Triangle upto n Rows

This program asks from user to define the size of Pascal's triangle, that is upto how many rows, he/she wants to print Pascal's triangle:

#include<stdio.h>
#include<conio.h>
long int fact(int);
int main()
{
    int i, c, rowLimit;
    printf("Enter the Number of Rows: ");
    scanf("%d", &rowLimit);
    for(i=0; i<rowLimit; i++)
    {
        for(c=(rowLimit-1); c>i; c--)
            printf(" ");
        for(c=0; c<=i; c++)
            printf("%ld ", fact(i)/(fact(c)*fact(i-c)));
        printf("\n");
    }
    getch();
    return 0;
}
long int fact(int n)
{
    int i, res=1;
    for(i=1; i<=n; i++)
        res = res*i;
    return res;
}

Here is its sample run:

print pascal triangle upto n rows c

Now supply the number of rows, that is upto how many rows, Pascal's triangle to expand. Let's suppose user has entered 8 as number of rows and pressed ENTER key. Then here is the output produced by above program:

pascal triangle upto n rows c

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker