Write a c program to print Pascal triangle







#include<stdio.h>

int main(){
  int length,i,j,k;

 //Accepting length from user
  printf("Enter the length of pascal's triangle : ");
  scanf("%d",&length);

 //Printing the pascal's triangle
  for(i=1;i<=length;i++) {
            for(j=1;j<=length-i;j++)
                        printf(" ");
            for(k=1;k<i;k++)
                        printf("%d",k);
            for(k=i;k>=1;k--)
                        printf("%d",k);
            printf("\n");
  }

  return 0;
}





5 comments:

  1. thi program shud give the pascals triangle for a limited number


    #include

    int fact(int n)
    { int ctr,pro=1;
    for(ctr=n;ctr>0;ctr--)
    pro*=ctr;
    return pro;
    }

    int combi(int n, int r)
    { int c;
    c=fact(n)/(fact(r)*fact(n-r));
    return c;
    }

    main( )
    { int n,ctr1,ctr2,r,num,cm;
    printf(“Enter the number of rows to be displayed :”);
    scanf(“%d”,&n);
    num=num;
    for(ctr1=0;ctr1<n;ctr++,num--)
    { for(r=0;r<=ctr1;r++)
    {cm=combi(ctr1,r);
    if(r= =0)
    { for(ctr2=0;ctr2<(2*num)-1;ctr2++)
    printf(“ “);
    printf(“%d”,cm);
    }

    else
    { for(ctr2=0;ctr2<3;ctr2++)
    printf(“ ”);
    printf(“%d”,cm);
    }
    }
    printf(“\n”);
    }
    }

    ReplyDelete
    Replies
    1. yes it will surely give pascal triangle upto a certain limit but this is much longer than the program for pascal triangle(mentioned above)

      Delete
  2. try this one


    #include
    #include
    main()
    {
    int length,i,j,k;
    //Accepting length from user
    printf("Enter the length of pascal's triangle : ");
    scanf("%d",&length);

    //Printing the pascal's triangle
    for(i=1;i<=length;i++) {
    for(j=1;j<=length-i;j++)
    printf(" ");
    for(k=1;k=1;k--)
    printf("%d",k);
    printf("\n");
    }

    getch();
    }

    ReplyDelete
  3. #include
    #include
    #include
    void main()
    {
    int i,j,k,n,p;
    printf("enter no of rows:");
    scanf("%d",&n);
    fflush(stdin);
    for(i=1;i<=n;i++)
    {
    for(j=1;j<=n-i-1;j++)
    printf(" ");
    for(k=1;k<=2*i-1;k++)
    p=pow(11,i-1);
    printf(" %d\t",p);
    printf("\n");
    }
    getch();
    }

    ReplyDelete