C language Yang hui triangle example

  • 2020-04-02 02:44:26
  • OfStack

In this paper, the example of C language to achieve the method of Yang hui triangle, to share for your reference.

The specific implementation method is as follows:


#include <iostream>

using namespace std;

void printYangHui(int line)
{
 int **array = new int*[line];
 for (int i = 0; i < line; i++)
 array[i] = new int[line];

 for (int i = 0; i < line; i++)
 {
 for (int j = 0; j <= i; j++)
 {
  if (j == 0 || j == i)
  {
  array[i][j] = 1;
  }
  else
  {
  array[i][j] = array[i - 1][j - 1] + array[i - 1][j];
  }
 }
 }

 for (int i = 0; i < line; i++)
 {
 for (int j = 0; j <= i; j++)
 {
  cout << array[i][j] << " ";
 }
 cout << endl;
 }
}

void main()
{
 printYangHui(5);
}

I hope that this paper is helpful to the learning of C programming algorithm design.


Related articles: