diff --git a/Java/projects/games/PascalsTriangle.java b/Java/projects/games/PascalsTriangle.java new file mode 100644 index 00000000..c812e0ac --- /dev/null +++ b/Java/projects/games/PascalsTriangle.java @@ -0,0 +1,27 @@ +// you are given the value of n and u have to display the nth row +// n=3 +// formula: nCr = n!/(r! * (n-r)!) + +import java.util.*; + +class PascalsTriangle { + + public static void main(String[] args) { + int n = 5; + List list = new ArrayList<>(); + for (int c = 1; c <= n; c++) { + list.add(nCr(n - 1, c - 1)); + } + System.out.println(list.toString()); + + } + + static int nCr(int r, int c) { + int result = 1; + for (int i = 0; i < c; i++) { + result = result * (r - i); + result = result / (i + 1); + } + return result; + } +}