Printing Pascal Triangle

Problem Statement
Write a c program to print Pascal's triangle
you should take the size of pascal triangle as an input from user
you should use only one array to store the variables

#include<stdio.h>
int main()
{
  static int a[20];
  int n,i,m,k;
  printf("enter a non negative integer\n");
  scanf("%d",&n);
  printf(" Pascal’s Triangle of order %d:\n",n);
  for(i=0;i<n+1;i++)
    {
      a[0]=1;/* initialising first variable */
      if(i>1)
{
 for(m=(i-1);m>0;m--)
   {  
     a[m]=(a[m]+a[m-1]);
   }
}
      if(i>0)      
{
 a[i]=1; /* giving value to last element of array */
}
      for(k=0;k<i+1;k++)
{
 printf("%d ",a[k]);
}
      printf("\n");
    }
  return 0;
}

No comments:

Post a Comment