- On
- By
- 0 Comment
- Categories: Array, do-while, for loop, Loop, Number pattern, While loop
Java code to Pascal triangle pattern using Array
Java code to Pascal triangle pattern using Array
In this tutorial, we will discuss the concept of the Java code to Pascal triangle pattern using Array
In this topic, we are going to learn how to write a program to print Pascal triangle patterns using a single dimension Array 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 declares and initializes integer variables, it will display a pascal triangle number pattern using for loop in the Java language according to the rows
Program 1
//Java programto print pascal triangle using Array //class diclaration class Disp_PascalTriangleArray{ public static void main (String args[]){//main method int row=6,i,j,temp=1,count=0; int[] array=new int[row]; int[] arrayTemp=new int[row]; array[0]=1; array[1]=1; for(i=0; i<row; i++) { for(j=(row-1); j>i; j--) System.out.print(" "); for(j=0; j<=i; j++) { 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++; } } } 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; } } } }
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 declares and initializes variables, it will show a pascal triangle number pattern using a while loop in the Java language according to the rows,
Program 2
//Java programto print pascal triangle using Array //class diclaration class Disp_PascalTriangleArray1{ public static void main (String args[]){//main method int row=5,i,j,temp=1,count=0; int[] array=new int[row]; int[] arrayTemp=new int[row]; array[0]=1; array[1]=1; i=0; while(i<row) { j=(row-1); while( j>i){ System.out.print(" "); 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 declares and initializes variables and then it will show a pascal triangle number pattern using the do-while loop in the Java language according to the rows,
Program 1
//Java programto print pascal triangle using Array //class diclaration class Disp_Pascal_Triangle_Array_Dowhile1{ public static void main (String args[]){//main method int row_No=7,i,j,temp=1,count=0; int[] array=new int[row_No]; int[] arrayTemp=new int[row_No]; array[0]=1; array[1]=1; i=0; do{ j=(row_No-1); do{ System.out.print(" ");//print initial space j--; }while( j>=i); j=0; do{ 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++; }while(j<=i); 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++; }while(i<row_No); } }
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
Similar post
Java program to print pascal triangle
C program to print pascal triangle
C++ program to print pascal triangle
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