2.01.2012

Pascal Triangle

Q. Write a C program to generate pascal triangle.
OR
Q. Write a C program to print following number triangle :




1






1
1




1
2
1


1
3
3
1
1
4
6
4
1


Ans.


How to build Pascal triangle:

To build the pascal triangle, start with "1" at the top, then continue placing numbers below it in a triangular pattern. Each number is build just sum of above two number, (except for the edge, which are all ‘1’ and all numbers outside the Triangle are 0's). so

0 row = 1

1 row = adding the two numbers above them to the left and the right

         = (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



/*c program for making pascal triangle*/
#include<stdio.h>
#include<conio.h>
long calc( int );
int main()
{
 int i,j,row,pas;
 printf("Enter no. of rows in pascal triangle : ");
 scanf("%d", &row);
 for(i=0; i<row; i++)
 {
   for(j=0; j<=(row-i-1); j++)
     printf(" ");
   for(j=0; j<=i; j++)
   {
     pas=calc(i)/(calc(j)*calc(i-j));
     printf("%ld ",pas); //take single space
   }
   printf("\n");
 }
 getch();
 return 0;
}

long calc( int num)
{
 int x;
 long res=1;
 for(x=1; x<=num; x++)
   res=res*x;
 return (res);
}

Output:-
Output of pascal triangle C program
Figure: Screen shot for pascal triangle C program

24 comments:

  1. simple way with out using function....

    #include
    #include

    void main()
    {
    int i,j,n,c,k,space;
    clrscr();
    printf("Enter the limit ");
    scanf("%d",&n);
    printf("\n\n");
    space=n;
    for(i=0;i<=n;i++)
    {
    c=1;
    for(k=space;k>=0;k--)
    printf(" ");

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

    ReplyDelete
    Replies
    1. good its so simple, thanks

      Delete
    2. Excellent ... and great effort... U proved u r a great programmer :)

      Delete
    3. it works good but if u enter the no of rows as 2,in the output three rows are printed i.e. if u enter the no rows as a,the output is printed with a+1 rows

      Delete
    4. @Aravind Swamy,

      I checked above program again and find that,
      the output will be equal of the entered no of rows.

      Try some different compiler.

      Delete
    5. thank u dinilps
      this program is working

      Delete
  2. i think the comment is the best method to solve it.the given is quite logical bt the next is ultimate.

    ReplyDelete
  3. the above prog is good bt u hv to declare the pass as long else it give u garbage value...

    ReplyDelete
  4. Simpler Approach!
    #include
    void main()
    {
    int i,j,x,n,s;
    printf("Enter The Number : ");
    scanf("%d",&n);
    for(i=0;i<=n;i++)
    {
    x=1;
    for(s=1;s<=n-i;s++)
    printf("\t");
    for(j=1;j<=i+1;j++)
    {
    printf("%d\t\t",x);
    x=x*(i-j+1)/j;
    }
    printf("\n");
    }
    }

    ReplyDelete
    Replies
    1. every thing is correct except that in:
      line 11 : printf(" ");
      line 14 : printf("%d ",x); //single space

      change these things in the program and it will run

      Delete
  5. its not printing the required output

    ReplyDelete
    Replies
    1. @artwel junior,

      Write down your code, for solving error.

      Try some other differ compiler.

      Delete
  6. #include
    #include
    void main()
    {
    int i,j,sp,num=1,r;
    clrscr();
    for(i=1;i<=5;i++)
    {
    r=num;
    for(sp=i;sp<=5;sp++)
    printf(" "); //2 space
    for(j=1;j<=i;j++)
    {
    printf("%d ",r%10); //3 space
    r=r/10;
    }
    num=num*11;
    printf("\n");
    }
    getch();
    }

    ReplyDelete
  7. can i have a flow chart and algorithm for this?

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. This comment has been removed by a blog administrator.

    ReplyDelete
  10. can any one explain this logic for me

    ReplyDelete
  11. #include
    #include

    void main()
    {
    int i,j,n,c,k,space;
    clrscr();
    printf("Enter the limit ");
    scanf("%d",&n);
    printf("\n\n");
    space=n;
    for(i=0;i<=n;i++)
    {
    c=1;
    for(k=space;k>=0;k--)
    printf(" ");

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

    ReplyDelete
  12. #include
    #include

    void main()
    {
    int i,j,n,c,k,space;
    clrscr();
    printf("Enter the limit ");
    scanf("%d",&n);
    printf("\n\n");
    space=n;
    for(i=0;i<=n;i++)
    {
    c=1;
    for(k=space;k>=0;k--)
    printf(" ");

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

    ReplyDelete
  13. #include
    #include
    long calc( int );
    int main()
    {
    int i,j,row,pas;
    printf("Enter no. of rows in pascal triangle : ");
    scanf("%d", &row);
    for(i=0; i<row; i++)
    {
    for(j=0; j<=(row-i-1); j++)
    printf(" ");
    for(j=0; j<=i; j++)
    {
    pas=calc(i)/(calc(j)*calc(i-j));
    printf("%ld ",pas); //take single space
    }
    printf("\n");
    }
    getch()

    ReplyDelete
  14. #include
    #include
    #include


    int fact(int a)
    {
    int b,fact=1;
    for(b=1;b<=a;b++)
    fact=fact*b;

    return fact;


    }




    main()
    {
    int a,b,c,row,ele;
    printf("Enter number of row you want to print:");
    scanf("%d",&row);
    for(a=0;a<=row;a++)
    {
    for(b=0;b<=(row-a+2);b++)
    {
    printf(" ");

    }
    for(c=0;c<=a;c++)
    {
    ele=fact(a)/(fact(c)*fact(a-c));
    printf("%d ",ele);
    }
    printf("\n");

    }



    }

    ReplyDelete