From 1b66c6a783a4f6d83e3b85990cd9c9a6faef4f4a Mon Sep 17 00:00:00 2001 From: Ramanujam <79093677+ramanujam001@users.noreply.github.com> Date: Wed, 12 Oct 2022 16:04:49 +0530 Subject: [PATCH] DSA_Pascal's_Triangle.cpp github userid : ramanujam001 aim :Pascals triangle is used widely in probability theory, combinatorics, and algebra. Generally, we can use Pascal's triangle to find the coefficients of binomial expansion, to find the probability of heads and tails in a toss, in combinations of certain things, etc. --- Pascal's_Triangle.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Pascal's_Triangle.cpp diff --git a/Pascal's_Triangle.cpp b/Pascal's_Triangle.cpp new file mode 100644 index 0000000..8a1c1ff --- /dev/null +++ b/Pascal's_Triangle.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; +int binomialCoeff(int n, int k); +void printPascal(int n) +{ + for (int line = 0; line < n; line++) + { + for (int i = 0; i <= line; i++) + cout <<" "<< binomialCoeff(line, i); + cout <<"\n"; + } +} +int binomialCoeff(int n, int k) +{ + int res = 1; + if (k > n - k) + k = n - k; + for (int i = 0; i < k; ++i) + { + res *= (n - i); + res /= (i + 1); + } + + return res; +} + +int main() +{ + int n = 7; + printPascal(n); + return 0; +}