SoFunction
Updated on 2025-03-07

Implementation of Yang Hui Triangle in C#

Implementation of Yang Hui Triangle in C#

Problem Description: Create a program to find triangles. The program prompts the user to enter data, and then displays the rules of Yang Hui's triangle.

// Input description: Yang Hui's triangle is long, representing the numerical value

// Program output: Yang Hui Triangle

using System;
using ;
using ;
using ;


namespace ConsoleApplication2
{
  class Program
  {
    static void Main(string[] args)
    {
      int length = 0;//The length of Yang Hui triangle      ("Input Yang Hui's triangle length:");


      length = Convert.ToInt32(());//Specify the length of Yang Hui triangle      int[][] a = new int[length][];//Two-dimensional array

      for (int i = 0; i < ; i++)
        a[i] = new int[i + 1];//Travel, assignment increment      for (int j = 0; j < ; j++)
      {
        a[j][0] = 1; //Assign all elements in column 1        a[j][j] = 1; // Assign 1 to the rightmost element of each column        for (int m = 1; m < a[j].Length - 1; m++)


          a[j][m] = a[j - 1][m - 1] + a[j - 1][m];//The values ​​of the other elements are calculated by Yang Hui's formula      }
      for (int i = 0; i < ; i++) //Transfer the array to output Yang Hui triangle      {


        for (int j = 0; j < a[i].Length; j++)
          ("{0}\t", a[i][j]);
        ("\n");
      }
      ();



    }
  }
}

If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!