C language applet Yang hui trigonometry sample code

  • 2020-04-02 01:12:54
  • OfStack


#include <stdio.h>
#include <stdlib.h>
int main()
{
 int i,j,k;
 int line;
 int *prev, *next;
 printf(" Enter to see the number of rows of the Yang hui triangle (greater than 2 ) :");
 scanf("%d",&line);
 if(line < 2)
 {
  printf(" Number of lines is less than 2 . Goodbye!n");
  exit(1);
 }
 for(i=1; i<=line; i++)   //Print the first two lines
  printf("   ");
 printf("%6dn",1);
 for(i=1; i<=line-1; i++)
  printf("   ");
 printf("%6d%6dn",1,1);
 prev = malloc(2*sizeof(int));
 prev[0] = 1;
 prev[1] = 1;
 for(i=3; i<=line; i++)   //Print from the third line
 {
  next = malloc(i*sizeof(int));
  next[0] = 1;
  next[i-1] = 1;
  for(j=line; j>=i; j--)    //The external Spaces
  {
   printf("   ");
  }
  printf("%6d",1);
  for(k=2; k<i; k++)    //digital
  {
   next[k-1] = prev[k-2] + prev[k-1];
   printf("%6d",next[k-1]);
  }
    }
  printf("%6dn",1);
  free(prev);
  prev = next;
 }
 free(next);
 return 0;
}

Related articles: