# Java Program to Print Pascal’s Triangle

«
»

Pascal triangle in Java is a matrix of the binomial coefficients. The number of entries in every row increases by 1 starting from the first row having 1 entry and the second row having 2 entries and so on.

Example:

```                           1  -------------> Top of the  triangle

1    1
\+/
1    2    1
\+/  \+/
1    3    3    1```
Problem Description

Write a Java program to print the pascal’s triangle of a given size.

Problem Solution

For every row of the matrix calculate the binomial coefficients C(row,0) to C(row, row) and display them.
The binomial coefficient can be calculated using the formula:

C(n,r) = n!/((n-r)!*r!)

There are several ways to print a pascal triangle program in Java. Let’s look at the different techniques to write a pascal triangle program.

Method 1: Print Pascal Triangle

A Java program to print Pascal’s triangle using nested loops and the binomial coefficient formula.

Program/Source Code

Here is the source code of the Java Program to print Pascal’s triangle using nested loops and the binomial coefficient formula. The program is successfully compiled and tested using the IDE IntelliJ Idea in Windows 7. The program output is also shown below.

```/*
* Java Program to print Pascal's triangle using nested loops
*/

public class PascalTriangle
{
public static void main(String[] args)
{
int rows = 5;
for (int i = 0; i < rows; i++)
{
int number = 1;

// Print initial spaces
for (int j = 0; j < rows - i; j++)
{
System.out.print(" ");
}
for (int j = 0; j <= i; j++)
{
System.out.print(number + " ");
number = number * (i - j) / (j + 1);
}
System.out.println();
}
}
}```
Program Explanation

1. This program prints Pascal’s triangle with the number of rows specified by the rows variable.
2. The triangle is printed using nested loops and the binomial coefficient formula.
3. Each number in the triangle is calculated and printed using the formula (i – j) / (j + 1), where i is the row number and j is the column number.

Time Complexity: O(n2)
The time complexity of the Pascal Triangle program is O(n2) because it uses nested loops to calculate the values of the triangle, where n is the number of lines.

Space Complexity: O(1)
The space complexity of the Pascal Triangle program is O(1), as it does not use any additional data structures that scale with the input size. It only uses a constant amount of memory to store the loop variables and the number variable.

Program Output:
```     1
1 1
1 2 1
1 3 3 1
1 4 6 4 1```

Method 2: Print Pascal Triangle using Recursion

This Java program prints Pascal’s triangle of a given size using a recursive approach

Program/Source Code

Here is the source code of the Java Program to print the Pascal’s Triangle of a given size. The program is successfully compiled and tested using the IDE IntelliJ Idea in Windows 7. The program output is also shown below.

```
//Java program to print Pascal's Triangle of a given size.

public class PascalTriangle {
// Function to calculate factorial of a number
static int factorial(int n)
{
int fact = 1;
int i;
for(i=1; i<n; i++)
{
fact*=i;
}
return i;
}
// Function to display the pascal triangle
static void display(int n)
{
int i;
int coefficient;
int line;
for(line=1;line<=n;line++)
{
for(i=0;i<=line;i++)
{
System.out.print((factorial(line)/factorial(line-i) * factorial(i)) + " ");
}
System.out.println();
}
}
// main Function to read user input
public static void main(String[] args){
int n;
System.out.println("Enter the size");
try {
}
catch(Exception e){
System.out.println("Invalid Input");
return;
}
System.out.println("The Pascal's Triangle is");
display(n);
}
}```
Program Explanation

1. In function display(), the loop for(line=1;line<=n;line++) is used to print n lines of the triangle.
2. The nested loop for(i=1;i<=line;i++) is used to display various coefficients.
3. The various coefficients are calculated using the factorial function, as mentioned in the problem solution.

Time Complexity: O(n2)
The time complexity of the Pascal Triangle program is O(n2) because it uses nested loops to calculate the values of the triangle, where n is the number of lines.

Space Complexity: O(1)
The space complexity is O(1) because the program only uses a few integer variables and does not store any data in a data structure.

Runtime Test Cases

Testcase 1: In this case, the entered value is “4” to print the pascal triangle.

```Enter the size
4
The Pascal's Triangle is
1
1 1
1 2 1
1 3 3 1```

Testcase 2: In this case, the entered value is “12” to print the pascal triangle.

```Enter the size
12
The Pascal's Triangle is
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
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1```

Method 3: Print Pascal Triangle Using 2D array

The program uses a 2D array to store the values of the triangle, calculates the values of the elements using a nested loop, and prints the triangle using nested loops.

Program/Source Code

Here is the source code of the Java Program to print the Pascal’s Triangle of a given size. The program is successfully compiled and tested using the IDE IntelliJ Idea in Windows 7. The program output is also shown below.

```import java.util.Scanner;

public class PascalTriangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
int[][] pascal = new int[rows][rows];

for(int i = 0; i < rows; i++) {
pascal[i][0] = 1;
}

for(int i = 1; i < rows; i++)
{
for(int j = 1; j < rows; j++) {
pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];
}
}

// Print the Pascal Triangle
for(int i = 0; i < rows; i++)
{
for(int j = 0; j <= i; j++)
{
System.out.println(pascal[i][j] + " ");
}
System.out.println();
}
}
}```
Program Explanation

1. The program asks the user how many rows they want in the triangle.
2. A 2D array is created with the specified number of rows and columns.
3. It uses a loop to create an empty triangle with the correct number of rows.
4. The first and last number of each row is 1.
5. The program uses another loop to fill in the rest of the numbers in each row.
6. Each number is the sum of the two numbers above it from the previous row.
7. The program uses a loop to print the triangle to the screen.

Time Complexity: O(n2)
The time complexity of the Pascal Triangle program is O(n2) because it uses nested loops to calculate the values of the triangle, where n is the number of lines.

Space Complexity: O(n2)
The space complexity is also O(n2) because it stores all the values in a 2D array.

Runtime Test Cases

Testcase 1: In this case, the entered value is “5” to print the pascal triangle.

```Enter the size
5
The Pascal's Triangle is
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1```

Testcase 2: In this case, the entered value is “10” to print the pascal triangle.

```Enter the size
10
The Pascal's Triangle is
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
1 9 36 84 126 126 84 36 9 1```

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.