Repository: TheAlgorithms/Java
Pull request #2871 (2021-12-10 09:27:13)

My Commit for the Issue #2864 Pascal's Triangle
### **Describe your change:**

- [x] Add an algorithm?
- [ ] Fix a bug or typo in an existing algorithm?
- [ ] Documentation change?

#### References

<!-- Add any reference to previous pull-request or issue -->

### **Checklist:**

<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All filenames are in all uppercase characters with no spaces or dashes.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
- [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.


iamhardikat11: Made my PR. Please Review and Respond.
iamhardikat11: This task (Build/build(pull_request)) is failing everytime I try, what can be done?
iamhardikat11: I have resolved all Checks please Merge my PR.

Patch:
diff --git a/src/main/java/com/thealgorithms/maths/PascalTriangle.java b/src/main/java/com/thealgorithms/maths/PascalTriangle.java
new file mode 100644
index 00000000..62ee3bc9
--- /dev/null
+++ b/src/main/java/com/thealgorithms/maths/PascalTriangle.java
@@ -0,0 +1,65 @@
+package com.thealgorithms.maths;
+
+import java.util.Scanner;
+public class PascalTriangle {
+    /**
+     *In mathematics, Pascal's triangle is a triangular array of the binomial coefficients that arises
+     * in probability theory, combinatorics, and algebra. In much of the Western world, it is named after
+     * the French mathematician Blaise Pascal, although other mathematicians studied it centuries before
+     * him in India, Persia, China, Germany, and Italy.
+     *
+     * The rows of Pascal's triangle are conventionally enumerated starting with row n=0 at the top (the 0th row).
+     * The entries in each row are numbered from the left beginning with k=0 and are usually staggered relative
+     * to the numbers in the adjacent rows. The triangle may be constructed in the following manner:
+     * In row 0 (the topmost row), there is a unique nonzero entry 1. Each entry of each subsequent row is
+     * constructed by adding the number above and to the left with the number above and to the right, treating
+     * blank entries as 0. For example, the initial number in the first (or any other) row is 1 (the sum of 0 and 1),
+     * whereas the numbers 1 and 3 in the third row are added to produce the number 4 in the fourth row. *
+     *
+     *<p>
+     *     link:-https://en.wikipedia.org/wiki/Pascal%27s_triangle
+     *
+     * <p>
+     *     Example:-
+     *                  1
+     *                1   1
+     *              1   2   1
+     *            1   3   3   1
+     *          1   4   6   4   1
+     *        1   5  10   10  5   1
+     *      1   6  15  20   15  6   1
+     *    1   7  21  35   35  21  7   1
+     *  1   8  28  56  70   56   28  8   1
+     *
+     */
+
+        public static int[][] pascal(int n)
+    {
+        /**
+         * @param arr  An auxiliary array to store generated pascal triangle values
+         * @return
+         */
+        int[][] arr = new int[n][n];
+        /**
+         * @param line Iterate through every line and print integer(s) in it
+         * @param i Represents the column number of the element we are currently on
+         */
+        for (int line = 0; line < n; line++)
+        {
+            /**
+             *  @Every line has number of integers equal to line number
+             */
+            for (int i = 0; i <= line; i++)
+            {
+                // First and last values in every row are 1
+                if (line == i || i == 0)
+                    arr[line][i] = 1;
+                    // The rest elements are sum of values just above and left of above
+                else
+                    arr[line][i] = arr[line-1][i-1] + arr[line-1][i];
+            }
+        }
+        
+        return arr;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java b/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java
new file mode 100644
index 00000000..93c87bd2
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java
@@ -0,0 +1,42 @@
+
+package com.thealgorithms.maths;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class PascalTriangleTest {
+
+	@Test
+	void testForOne() 
+	{
+		int[][] result = PascalTriangle.pascal(1);
+        int[][] expected = {{1}};
+		assertArrayEquals(result,expected);
+	}
+
+	@Test
+	void testForTwo() 
+	{
+		int[][] result = PascalTriangle.pascal(2);
+    	int[][] expected = {{1,0},{1,1}};
+		assertArrayEquals(result,expected);
+	}
+
+	@Test
+	void testForFive() 
+	{
+		int[][] result = PascalTriangle.pascal(5);
+    	int[][] expected = {{1,0,0,0,0},{1,1,0,0,0},{1,2,1,0,0},{1,3,3,1,0},{1,4,6,4,1}};
+		assertArrayEquals(result,expected);
+	}
+
+	@Test
+	void testForEight() {
+		int[][] result = PascalTriangle.pascal(8);
+    	int[][] expected = {{1,0,0,0,0,0,0,0},{1,1,0,0,0,0,0,0},{1,2,1,0,0,0,0,0},{1,3,3,1,0,0,0,0},{1,4,6,4,1,0,0,0},{1,5,10,10,5,1,0,0},{1,6,15,20,15,6,1,0},{1,7,21,35,35,21,7,1}};
+		assertArrayEquals(expected, result);
+	}
+
+
+}
diff --git a/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java b/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java
index 2efebb08..773a5aab 100644
--- a/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java
+++ b/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java
@@ -12,21 +12,21 @@ class ArrayLeftRotationTest {
 		int[] result = ArrayLeftRotation.rotateLeft(arr, 3);
 		assertArrayEquals(arr, result);
 	}
-	
+
 	@Test
 	void testForZeroStep() {
 		int[] arr = {3, 1, 5, 8, 6};
 		int[] result = ArrayLeftRotation.rotateLeft(arr, 0);
 		assertArrayEquals(arr, result);
 	}
-	
+
 	@Test
 	void testForEqualSizeStep() {
 		int[] arr = {3, 1, 5, 8, 6};
 		int[] result = ArrayLeftRotation.rotateLeft(arr, 5);
 		assertArrayEquals(arr, result);
 	}
-	
+
 	@Test
 	void testForLowerSizeStep() {
 		int[] arr = {3, 1, 5, 8, 6};
@@ -35,7 +35,7 @@ class ArrayLeftRotationTest {
 		int[] result = ArrayLeftRotation.rotateLeft(arr, n);
 		assertArrayEquals(expected, result);
 	}
-	
+
 	@Test
 	void testForHigherSizeStep() {
 		int[] arr = {3, 1, 5, 8, 6};
@@ -45,4 +45,4 @@ class ArrayLeftRotationTest {
 		assertArrayEquals(expected, result);
 	}
 
-}
+}
\ No newline at end of file