- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to generate pascals triangle for the given number using C#?
Pascal’s Triangle is a number pattern in the shape of a triangle. Pascal’s Triangle has many applications in mathematics and statistics, including its ability to help you calculate combinations.
Each number in the triangle is the sum of the two numbers above it. For example, row 4 − it’s the sum of 3 and 3 in the row above. The very first and very last number in any row are always going to be 1.
Time complexity − O(N)
Space complexity − O(N)
Example
public class Arrays{ public List<List<int>> GeneratePascal(int n){ List<List<int>> res = new List<List<int>>(); if (n <= 0){ return null; } List<int> first = new List<int>(); first.Add(1); res.Add(first); if (n == 1){ return res; } for (int i = 2; i < n; i++){ List<int> prev = res.LastOrDefault(); List<int> cur = new List<int>(); for (int temp = 0; temp < i; temp++){ cur.Add(1); } for (int j = 1; j < i - 1; j++){ cur[j] = prev[j - 1] + prev[j]; } res.Add(cur); } return res; } } static void Main(string[] args){ Arrays s = new Arrays(); var res = s.GeneratePascal(5); }
Output
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
- Related Articles
- Golang Program To Print Star Pascals Triangle
- C++ Program to Generate a Random UnDirected Graph for a Given Number of Edges
- C++ Program to Generate a Random Directed Acyclic Graph DAC for a Given Number of Edges
- How to generate a random number in C++?
- Find 2’c complements for given binary number using C language
- C++ Program to Generate a Graph for a Given Fixed Degree Sequence
- How to generate the first 100 Odd Numbers using C#?
- How to generate the first 100 even Numbers using C#?
- How to generate a string randomly using C#?
- C++ program to generate random number
- C++ Program to Generate Prime Numbers Between a Given Range Using the Sieve of Sundaram
- C program to rotate the bits for a given number
- How to find the power of any given number by backtracking using C#?
- C++ Program to Generate a Sequence of N Characters for a Given Specific Case
- How to generate an array for bi-clustering using Scikit-learn?

Advertisements