Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Pascal triangle programming in C#

Pascal triangle calculation is showing the triangle with the multiplication of 11 of numbers diagonally. (Starting with one)


  1. using System;
  2.  
  3. namespace forgetCode
  4. {
  5. class Program
  6. {
  7. public static void Main()
  8. {
  9. int binom = 1, q = 0, r, x;
  10. //Input from the user
  11. Console.WriteLine("Enter the number of rows");
  12. string s = Console.ReadLine();
  13. int p = Int32.Parse(s);
  14. Console.WriteLine("The Pascal Triangle");
  15. while (q < p)
  16. {
  17. //Pascal programming
  18.  
  19. for (r = 40 - (3 * q); r > 0; --r)
  20. Console.Write(" ");
  21. for (x = 0; x <= q; ++x)
  22. {
  23. if ((x == 0) || (q == 0))
  24. binom = 1;
  25. else
  26. binom = (binom * (q - x + 1)) / x;
  27. Console.Write(binom);
  28. }
  29. Console.Write("\n ");
  30. ++q;
  31. }
  32. }
  33. }
  34.  
  35. }


Output:
Enter the number of rows
5
The Pascal Triangle
1
11
121
1331
14641

(But the output is actually shown in diagonally)

..