Print Star Pascal's Triangle in Java



In this article, we will understand how to print star Pascal's triangle in Java. The Pascal's triangle is formed by using multiple for-loops and print statements. All values outside the triangle are considered zero (0). The first row is 0 1 0 whereas only 1 acquire a space in Pascal's triangle, 0s are invisible. The second row is acquired by adding (0+1) and (1+0). The output is sandwiched between two zeroes. The process continues till the required level is achieved.

Problem Statement

Write a program in Java to print star Pascal's triangle. Below is a demonstration of the same ?

Input

Enter the number of rows in Pascal's Triangle : 8

Output

Enter the number of rows in Pascal's Triangle : 8
The Pascal's Triangle :
        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

Using user input

Following are the steps to print the star Pascal's triangle ?

  • Import the Scanner class from java.util package.
  • We will define methods and calculate the factorial of a number and will compute binomial coefficients.
  • Create Scanner object to get the user input and will user and use the nextInt() to get the number of rows.
  • Generate Pascal's triangle using nested loops to format and calculate values dynamically.

Example

Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool run button.

import java.util.Scanner;
public class PascalsTriangle {
   static int factorial(int my_input) {
      int factors;
      for(factors = 1; my_input > 1; my_input--){
         factors *= my_input;
      }
      return factors;
   }
   static int combination(int my_input,int r) {
      return factorial(my_input) / ( factorial(my_input-r) * factorial(r) );
   }
   public static void main(String args[]){
      System.out.println();
      int my_input, i, j;
      my_input = 5;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the number of rows in Pascal's Triangle : ");
      my_input = my_scanner.nextInt();
      System.out.println("The Pascal's Triangle : ");
      for(i = 0; i <= my_input; i++) {
         for(j = 0; j <= my_input-i; j++){
            System.out.print(" ");
         }
        for(j = 0; j <= i; j++){
           System.out.print(" "+combination(i, j));
        }
        System.out.println();
     }
   }
}

Output

Required packages have been imported
A reader object has been defined
Enter the number of rows in Pascal's Triangle : 8
The Pascal's Triangle :
        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

Using predefined input

Following are the steps to print the star Pascal's triangle ?

  • Define factorial and combination methods for calculations.
  • Set the number of rows directly in the code.
  • Print Pascal's triangle using nested loops to format and calculate values with predefined rows.

Example

Here, the integer has been previously defined, and its value is accessed and displayed on the console ?

public class PascalsTriangle {
   static int factorial(int my_input) {
      int factors;
      for(factors = 1; my_input > 1; my_input--){
         factors *= my_input;
      }
      return factors;
   }
   static int combination(int my_input,int r) {
      return factorial(my_input) / ( factorial(my_input-r) * factorial(r) );
   }
   public static void main(String args[]){
      System.out.println();
      int my_input, i, j;
      my_input = 8;
      System.out.println("The number of rows in Pascal's Triangle is defined as " +my_input);
      System.out.println("The Pascal's Triangle : ");
      for(i = 0; i <= my_input; i++) {
         for(j = 0; j <= my_input-i; j++){
            System.out.print(" ");
         }
         for(j = 0; j <= i; j++){
            System.out.print(" "+combination(i, j));
         }
         System.out.println();
      }
   }
}

Output

The number of rows in Pascal's Triangle is defined as 8
The Pascal's Triangle :
            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
Updated on: 2024-09-09T01:19:20+05:30

676 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements