Pascal Triangle in JAVA

Pascal triangle is one of the most interesting Number Pattern. Let me tell you First what is Pascal Triangle and then we will move to it code.
  1. In Pascal Triangle each and every row of triangle contain the number of element equal to it’s row number
  2. For every row First and last element must be equal to 1 and rest of the element is the direct sum of above two element.
  3. The sum of element of a row must be equal to the exponent of 2. (Ex: 20 = 1 , 21 = 2 ….)




Let’s go through the logic to create Pascal Triangle:

  • Step - 1 Get the number of rows from user for the Pascal triangle.
  • Step - 2 Create a 2D array of size equal to number of rows which hold the value of Pascal Triangle and print them accordingly.
  • Step - 3 Iterate from First row to last.
    • Step - 4 Get the Number of Spaces and print them.
      • Step - 5 Number of Spaces in a row = Total number of Row - Current Row Number.
    • Step - 6 Not Iterate For Columns from First to last.
      • Step -7 If the Column == Current row OR Column == 0 then print 1 as the First and Last element of Every Row must be equal to 1 in Pascal Triangle. Column = 0 means it is the First element of row and Column == Current Row means that it is the last Element of row.
      • Step - 8 For the rest of the Element Get the above row two element directly above the currentRow element add them (as Shown in above figure 1 + 1 = 2 , 1 + 2 =3 , 2+ 1 = 3 and so on...) and print.
    • Step - 9 Give a line break in order to go into the next line.


This Example use printf function just to format the String and to give the equal spacing between the Elements of row. “%4f” simply means print the integer element and give 3 space after element.


Code for Pascal Triangle in Java:

package com.missTechy; import java.util.Scanner; public class PascalTriangle { public static void main(String[] args) { System.out.println("Enter the number of rows for Triangle : "); // Scanner class is used to get user input. Scanner sc = new Scanner(System.in); // variable rows contain the number of rows of Triangle entered by the user. int rows = sc.nextInt(); // values[][] is a 2D array to store Pascal triangle values. int[][] values = new int[rows][rows]; // Iterate through every line and print values in it. for (int currentRow = 0; currentRow < rows; currentRow++) { // Get the number of spaces and print them. for(int space = 1; space < rows - currentRow; space++) { System.out.print(" "); } // Every line has number of integers equal to line number for (int i = 0; i <= currentRow; i++) { // First and last values in every row are 1 if (currentRow == i || i == 0) { values[currentRow][i] = 1; } // Other values are sum of values just above and left of above else { values[currentRow][i] = values[currentRow-1][i-1] + values[currentRow-1][i]; } //printf is used to format String , "%4d" is used to print 3 space after a digit. System.out.printf("%4d", values[currentRow][i]); } // Used this to entered into next line. System.out.println(""); } } }

Output of Pascal Pattern in Java:

Post a Comment

0 Comments