Pages

  • Home
  • About & Contact
  • Archive
  • C Programming Examples

11 February 2012

c program to print pascal triangle [using arrays and for loop]

Here I am showing two ways to print the pascal triangle.

This is the program to print the pascal triangle using arrays and the program below than this program is the program to print pascals triangle using for loop. 


First way
#include<stdio.h>
#include<conio.h>
void main()
{
        int arr[30],temp[30],i,j,k,l,n;           //using 2 arrays
       clrscr();
     
       printf("Enter the number of lines to be printed: ");
       scanf("%d",&n);

       temp[0]=1;
       arr[0]=1;

       for(j=0;j<n;j++)
     printf(" ");

       printf(" 1\n");

       for(i=1;i<n;i++)
       {
               for(j=0;j<n-i;j++)
       printf(" ");

       for(k=1;k<i;k++)
       {
                  arr[k]=temp[k-1]+temp[k];      
        }

                arr[i]=1;

                for(l=0;l<=i;l++)
       {
                 printf("%2d",arr[l]);
                 temp[l]=arr[l];
        }

                printf("\n");
        }
       getch();
}

This program can also be done by using two-dimensional array instead of using 2 different arrays.

Expanation: Firstly I recommend you to see how does pascal triangle works.
In this program i have use the property of pascals triangle that sum of two numbers in above row i.e (sum of n-1 and n of above column is current n value).I have highlighted that line with red colour.here i have used temp array to store the values of previous line so that I can use it for getting the sum.


Second way
program to print pascals triangle using for loop.


#include<stdio.h>
#include<conio.h>

int fact(int);

void main()
{
      int i,j,n,fac;
      printf("Enter the number of lines to be printed");
      scanf("%d",&n);

      for(i=0;i<n;i++)
     {
            for(j=0;j<n-i-1;j++)
            {
                    printf(" ");
            }
            for(j=0;j<=i;j++)
            {
                      fac=fact(i)/(fact(j)*fact(i-j));
                      printf("%2d",fac);
            }
            printf("\n");
     }
     getch();
}


fact(int n)
{
      int i,f=1;
      for(i=n;i>=2;i--)
     {
          f = f * i;
     }
      return f;
}

Explanation:
I have just applied formula
    n!
--------
r!(n-r)!

for more pattern programs click here


Share your views regarding the given post via the comments section given below.
If you liked the post, please '+1' it & share it on other social networks.
Thank You.
-Romil Shah

11 comments:

  1. agree nice but could be better explained

    ReplyDelete
  2. the 2nd method goes invalid when no. of lines in pascal's triangle exceeds 100 as it is not easy to store 100!

    ReplyDelete
  3. how to print beyond 13 lines using second code?? Here its printing up to 13 lines correctly!!!

    ReplyDelete
  4. Anonymous15 May, 2013

    it certainly has helped me

    ReplyDelete
  5. hey how can i print numbers in descending order?I want this output:

    11
    2112
    321123

    ReplyDelete
  6. how can a c program be written for an output lik this
    1
    12
    123
    1234
    12345

    ReplyDelete
  7. /* Pascal Triangle using Array */
    #include
    #include
    int b[100],a[100],row,m,n,i=0,j=0,last=0;
    void space();
    void main()
    {
    int index;
    void print();
    void inc();
    clrscr();
    printf("Enter the rows\n");
    scanf("%d",&n);
    m=n;
    for(i=0;i<m;i++)
    {
    printf(" ");
    }
    printf("1\n");
    for(row=2,i=0;row<=n;row++)
    {
    a[i]=1;
    last++;
    a[last]=1;
    space();
    print();
    inc();
    printf("\n");
    }
    getch();

    }
    void print()
    {
    for(i=0;i<=last;i++)
    {
    printf("%d ",a[i]);
    }
    }

    void inc()
    {
    b[0]=1;
    for(i=0,j=1;i<=last;i++,j++)
    {
    b[j]=a[i]+a[i+1];
    }
    for(i=1;i<=last;i++)
    {
    a[i]=b[i];
    }
    }
    void space()
    {
    for(i=0;i<m-1;i++)
    printf(" ");
    m--;
    }

    ReplyDelete
  8. #include
    #include
    void main()
    {
    int s,i,j,l,n,a[10][10];
    clrscr();
    printf("enter n val");
    scanf("%d",&n);
    s=n;
    for(j=1;j<=n;j++)
    {
    for(l=1;l<=s-j;l++)
    printf(" ");
    for(i=1;i<=j;i++)
    {
    if(i==1||i==j)
    {
    printf("1");
    a[j][i]=1;
    }
    else if(j==i+1)
    {
    printf("%d",j-1);
    a[j][i]=j-1;
    }
    else if(j>3){
    a[j][i]=a[j-1][i-1]+a[j-1][i];
    printf("%d",a[j][i]);
    }
    printf(" ");
    }
    printf("\n");
    }
    getch();
    }

    ReplyDelete
  9. I've been looking for this all day, you really saved my life, thanks a bunch !!!!!!

    ReplyDelete

Back To Top