Menu BAR

FEEL FREE TO ASK ANY PROGRAM and please mention any error if you find it

1 Feb 2013

PASCAL TRIANGLE USING C PROGRAM

Q.) Write a C program to print PASCAL Triangle without using array
                                                or
      Write a C program to print PASCAL Triangle using for loop.

Sol )

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

long fact(int) ;
int main()
{
      int line,i,j ;
      printf("Enter the number of lines : ") ;
      scanf("%d",&line) ;
   
      for(i=0;i<line;i++)
          {
               for(j=0;j<line-i-1;j++)
                         printf("  ");

               for(j=0;j<=i;j++)
                         printf("%ld ",fact(i)/(fact(j))*fact(i-j))) ;
               printf("\n") ;
           }
       return 0;
}

long fact(int num)
{
          long f=1;
          int i=1;
          while(i<=num)
             {
                   f=f*i ;
                   i++ ;
              }
          return f;
}

                                :: SAMPLE OUTPUT ::
Enter the number of lines : 6
                           1
                       1       1
                     1    2    1
                  1    3    3   1
                1   4     6    4   1
              1  5  10    10   5  1

1 comment:

  1. Thank you so much For your support. keep commenting :)

    ReplyDelete