- On
- By
- 1 Comment
- Categories: Array, do-while, for loop, Loop, Number pattern, While loop
Java code to generate pascal triangle using arrays with user input
Java code to generate pascal triangle using arrays with user input
In this tutorial, we will discuss the concept of the Java code to generate a pascal triangle using arrays with user input
In this topic, we are going to learn how to write a program to print Pascal triangle patterns using numbers using user input in the Java programming language
Here, we use for, while, and do-while loops for printing pascal triangle

Display the pascal triangle in Java using loops
Java Code to display pascal triangle using for loop
In this program, the user is asked to enter the number of rows and then it will show a pascal triangle number pattern using for loop in the Java language
Program 1
//Java programto print pascal triangle using Array
import java.util.Scanner;
//class diclaration
class Disp_Pascal_Triangle_Arr1{
public static void main (String args[]){//main method
Scanner scan=new Scanner(System.in);
//Scanner object for user input
System.out.print("Enter the row for pascal triangle: ");
//Ask input from the user for row
int row=scan.nextInt();//read the input from the user
int i,j,temp=1,counter=0;//declare variables
int[] array=new int[row];//declare one D array
int[] arrayTemp=new int[row];//declare temp one D array
array[0]=1;
array[1]=1;
for(i=0; i<row; i++)//outer for loop
{
for(j=(row-1); j>i; j--)//inner for loop
System.out.print(" ");//print initial space
for(j=0; j<=i; j++)//inner for loop
{
if(i==0)
System.out.print("1");
else
{ if(j==0|| j==i)
System.out.print("1 ");
else{
arrayTemp[temp]=array[counter]+array[counter+1];
System.out.print(arrayTemp[temp]+" ");
//print elemenys
temp++;
counter++;
}
}
}
System.out.println();//mpve to next line
arrayTemp[temp]=1;
if(i>1)
{
counter=0;
array[counter]=1;
for(temp=1,counter=1; counter<=i; temp++, counter++)
array[counter]=arrayTemp[temp];
temp=1;
counter=0;
}
}
}
}
When the above code is executed, it produces the following result
Java Code to display pascal triangle using while loop
In this program, the user is asked to enter the number of rows and then it will show a pascal triangle number pattern using the while loop in the Java language
Program 2
//Java programto print pascal triangle using Array
import java.util.Scanner;
//class diclaration
class Disp_PascalTriangleArraywhile{
public static void main (String args[]){//main method
Scanner scan=new Scanner(System.in);
//Scanner object for user input
System.out.print("Enter the row for pascal triangle: ");
//Ask input from the user for row
int rows=scan.nextInt();//read the input from the user
int row=5,i,j,temp=1,count=0;
int[] array=new int[rows];//declare single dim array
int[] arrayTemp=new int[rows];//declare single dim temp array
array[0]=1;
array[1]=1;
i=0;
while(i<rows)
{
j=(rows-1);
while( j>i){
System.out.print(" ");//print initial space
j--;
}
j=0;
while(j<=i)
{
if(i==0)
System.out.print("1");
else
{ if(j==0|| j==i)
System.out.print("1 ");
else{
arrayTemp[temp]=array[count]+array[count+1];
System.out.print(arrayTemp[temp]+" ");
temp++;
count++;
}
}
j++;
}
System.out.println();
arrayTemp[temp]=1;
if(i>1)
{
count=0;
array[count]=1;
for(temp=1,count=1; count<=i; temp++, count++)
array[count]=arrayTemp[temp];
temp=1;
count=0;
}
i++;
}
}
}
When the above code is executed, it produces the following result
Java Code to display pascal triangle using do-while loop
In this program, the user is asked to enter the number of rows and then it will show a pascal triangle number pattern using the do-while loop in the Java language
Program 3
//Java programto print pascal triangle using Array
import java.util.Scanner;
//class diclaration
class Disp_Pas_Tri_Arr_Dowhile_UserInput{
public static void main (String args[]){//main method
Scanner scan=new Scanner(System.in);
//Scanner object for user input
System.out.print("Enter the row for print triangle: ");
//Ask input from the user for row
int No_Of_rows=scan.nextInt();//read the input from the user
int i,j,temp=1,count=0;//integer variable declaration
int[] arr=new int[No_Of_rows];
//declare single dim array
int[] arrTemp=new int[No_Of_rows];
arr[0]=1;
arr[1]=1;
//initialize array elements
i=0;
do{//outer do-while loop
j=(No_Of_rows-1);
do{//inner do-while loop
System.out.print(" ");//print initial space
j--;
}while(j>=i);
j=0;
do{//inner do-while loop
if(i==0)
System.out.print("1");
else
{ if(j==0|| j==i)
System.out.print("1 ");
else{
arrTemp[temp]=arr[count]+arr[count+1];
System.out.print(arrTemp[temp]+" ");
temp++;
count++;
}
}
j++;
}while(j<=i);
System.out.println();
arrTemp[temp]=1;
if(i>1)
{
count=0;
arr[count]=1;
for(temp=1,count=1; count<=i; temp++, count++)
arr[count]=arrTemp[temp];
temp=1;
count=0;
}
i++;
}while(i<No_Of_rows);
}
}
When the above code is executed, it produces the following result
Suggested for you
Data type and variable in Java language
The operator in the Java language
do-while loop in Java language
Similar post
Java program to print pascal triangle
C program to print pascal triangle
C++ program to print pascal triangle
Java program to print pascal triangle using an array
Java program to print pascal triangle using array using user input
C code to Alphabet triangle pattern using the do-while loop
C++ code to Alphabet triangle pattern using the do-while loop
Java code to Alphabet triangle pattern using the do-while loop
Alphabet pattern in C language
Alphabet triangle pattern in C language using while loop
Alphabet pattern in Java language
Alphabet triangle pattern in Java language using while loop
1 Comment
aaa September 12, 2022 at 9:01 pm