Monday 17 September 2012

Pascal Triangle in c:


DESCRIPTION:
Pascal Triangle in c: C program to print Pascal triangle which you might have studied in Binomial Theorem in Mathematics. Number of rows of Pascal triangle to print is entered by the user. First four rows of Pascal triangle are shown below :-

   1
  1 1
 1 2 1
1 3 3 1
PROGRAM:
Pascal triangle in c
#include<stdio.h>

long factorial(int);

main()
{
   int i, n, c;

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

   for ( i = 0 ; i < n ; i++ )
   {
      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 );
}
ENTER THE NUMBER OF ROWS YOU WISH TO SEE IN PASCAL TRIANGLE5
      1
     1 1
    1 2 1
   1 3 3 1
  1 4 6 4 1
source code of pattern below:

   1
  121
 12321
1234321 
#include<stdio.h>

main()
{
    int n, c, k, number = 1, space = n;

    printf("Enter number of rows\n");
    scanf("%d",&n);

    space = n;

    for ( c = 1 ; c <= n ; c++ )
    {
        for ( k = space ; k > 1 ; k-- )
            printf(" ");

        space--;

        for ( k = 1 ; k <= 2*c - 1 ; k++ )
        {
            if ( k <= c)
            {
                 printf("%d", number);

                 if ( k < c )
                 number++;
            }
            else
            {
                number--;
                printf("%d", number);
            }
        }

        number = 1;
        printf("\n");
    }

    return 0;
}

No comments:

Post a Comment