Pascals Triangle by recursion

Posted by Olpers on Stack Overflow See other posts from Stack Overflow or by Olpers
Published on 2012-12-01T13:34:54Z Indexed on 2012/12/01 23:04 UTC
Read the original article Hit count: 201

Filed under:

Note : My Class Teacher gave me this question as an assignment... I am not asked to do it but please tell me how to do it with recursion

Binomial coefficients can be calculated using Pascal's triangle:

            1                   n = 0
         1     1
      1     2     1
   1     3     3     1
1     4     6     4     1       n = 4

Each new level of the triangle has 1's on the ends; the interior numbers are the sums of the two numbers above them.

Task: Write a program that includes a recursive function to produce a list of binomial coefficients for the power n using the Pascal's triangle technique. For example,

Input = 2 Output = 1 2 1

Input = 4 Output = 1 4 6 4 1

done this So Far but tell me how to do this with recursion...

#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;    
}

© Stack Overflow or respective owner

Related posts about c++