c program to print Pascal triangle

Pascal’s triangle in c :

SOLUTION :

#include<stdio.h>

long factorial(int);

int main()
{
   int i, n, c;

   printf("\n Enter the number of rows in pascal triangle to print : ");
   scanf("%d",&n);

   printf("\n RESULT : \n");
   for ( i = 0 ; i < n ; i++ )
   {
       printf(" ");
       for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
        printf(" ");

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

   return 0;
}

long factorial(int n)
{
   int c;
   long result = 1;

   for( c = 1 ; c <= n ; c++ )
         result = result*c;

   return ( result );
}

OUTPUT OF PROGRAM :

pascal-triangle

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s