Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Program To Generate Pascal Triangle

In this tutorial, we will learn how to generate a Pascal Triangle in 1D array. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java. For example,

Input: Number of Rows: 5

Output:

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

Program 1: Generate The Pascal Triangle

In this approach, we will see how to generate the Pascal Triangle using an array.

Algorithm

  1. Start
  2. Declare a variable for the number of rows.
  3. Ask the user to initialize the number of rows.
  4. Declare a 1D array.
  5. Use three for loop to generate the pascal triangle.
  6. Use the first outer for loop to iterate through all the rows.
  7. Use the second for loop to print the space.
  8. Assign the first element of each row as 1.
  9. Use the third for loop to print the elements.
  10. Display the Pascal Triangle.
  11. Stop

Below is the code for the same.

The below program demonstrates how to generate a Pascal Triangle.

/*JAVA PROGRAM TO GENERATE PASCAL TRIANGLE IN 1D ARRAY */
import java.util.*;

public class PascalTriangle
{
     public static void main(String []args)
     {
         Scanner sc=new Scanner(System.in);   //Take input from the user
         int i, j, k, l, r;            //Declarig Variabless 
          int a[]=new int[30];     //Declare a 1d array
         
         System.out.println("Enter the number of rows ");
         r=sc.nextInt();      //Initialize the number of rows
    
         //For Pascal Triangle
         for(i=0;i<r;i++)   //Iterate through all the rows
		 {
			for(k=r; k>i; k--)    //Print the number of spaces
			{
				System.out.print(" ");
			}
            a[i] = 1;   //Initialize the first element of each row as 1
			for(j=0;j<=i;j++)    //To find the Pascal triangle element
			{
				 System.out.print(a[i]+ " ");    //Print the array elements
                 a[i] = a[i] * (i - j) / (j + 1);   //Store the pascal triangle elements in an array
			}
			System.out.println();   //To move to the next line
		 }
        
     }
}


Enter the number of rows 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Program 2: Generate The Pascal Triangle

In this approach, we will see how to generate the Pascal Triangle using two array.

Algorithm

  1. Start
  2. Declare a variable for the number of rows.
  3. Ask the user to initialize the number of rows.
  4. Declare two arrays.
  5. Print 1 for the first element of the first row.
  6. Initialize the first element of both the arrays as 1.
  7. Use four for loops for the same.
  8. Use the first for loop to iterate through all the rows.
  9. Use the second for loop to print the space.
  10. Use the third for loop to initialize the numbers.
  11. Use the fourth for loop to print the numbers.
  12. Display the final output.
  13. Stop

Below is the code for the same.

/*JAVA PROGRAM TO GENERATE PASCAL TRIANGLE IN 1D ARRAY */
import java.util.*;

public class PascalTriangle
{
     public static void main(String []args)
     {
         Scanner sc=new Scanner(System.in);     //Take input from the user
         int i, j, k, l;            //Declarig Variabless 
          int array[]=new int[30];     //using 1d array
          int temp[]=new int[30];       //using 1d array
          
         int num;    //Declaring variable for the number of rows
         System.out.println("Enter the number of rows ");
         num=sc.nextInt();      //Initialize the number of rows
         
         temp[0] = 1;     //Initializing first variable of the array as 1
         array[0] = 1;   //Initializing first variable of the array as 1
    
    System.out.println("1");     //For first element
    for (i = 1; i < num; i++)      //To iterate through all the rows 
    {
        for (j = 0; j < i; j++)    //To print the space
        System.out.print("");
        for (k = 1; k < num; k++)
        {
            array[k] = temp[k - 1] + temp[k];      //Initialize the array to store the pascal triangle elements
        }
        array[i] = 1;
        for (l = 0; l <= i; l++)
        {
            System.out.print(array[l]+" ");  //Print the array elements
            temp[l] = array[l];    //Copy the array elements to another array
        }
        System.out.println("");    //For next line
    }
        
     }
}


Enter the number of rows 6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1