Array

Pascal’s triangle in Java using 2D Array

Pascal’s triangle in Java using 2D Array

In this tutorial, we will discuss the Title of Pascal’s triangle in Java using 2D Array

In this post, we will learn how to write code to display the pascal triangle number pattern in Java language using 2 D array with for, while, and do-while loop

Pascal triangle

Java program to print pascal pattern using 2 D Array

Program to display pascal triangle Using for loop

Program 1

This program allows the user to enter the number of rows,  and then the program will display the pascal triangle number pattern using for loop in the Java language

//Java program to show pascal's triangle using 2D Array
import java.util.Scanner;
//class diclaration
class Disp_Pascal_Triangle_2DArr1{
public static void main (String args[]){//main method
Scanner scan=new Scanner(System.in);
//Scanner object for user input
System.out.print("Enter the Number of rows: ");
//Ask input from the user for row
int rows=scan.nextInt();
//read the input from the user
int i=0,j=0,num=rows-1;
//declare and initiates variables
int arr[][]=new int[50][50];
//declare two D array
for(i=0; i<rows; i++){
arr[i][i]=arr[i][0]=1;
  }
    for(i=0; i<rows; i++){
            for(j=1; j<i; j++){
             arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
}
}
            
    for(i=0; i<rows; i++){
            for(j=0; j<num; j++){
            System.out.print(" ");
}
for(j=0; j<=i; j++){
            System.out.print(arr[i][j]+" ");
}
System.out.println();
num--;
}
  }
}

When the above code is executed, it produces the following result

Output 1

 

Program to display pascal triangle Using while loop

Program 1

This program allows the user to enter the number of rows,  and then the program will display the pascal triangle number pattern using the while loop in the Java language

//Java programto print pascal triangle using 2D Array
import java.util.Scanner;
//class diclaration
class Disp_Pascal_Triangle_2DArrWhile{
public static void main (String args[]){//main method
Scanner scan=new Scanner(System.in);
//Scanner object for user input
System.out.print("Enter the Number of rows: ");
//Ask input from the user for row
int rows=scan.nextInt();
//read the input from the user
int i=0,j=0,num=rows-1;
//declare initialize variables
int arr[][]=new int[50][50];
//declare two Dim array
i=0;
while(i<rows){
arr[i][i]=arr[i][0]=1;
 i++;
  }
  i=0; 
   while(i<rows){
     j=1;
            while(j<i){
             arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
        j++;
}
 i++;
}
      i=0;       
    while(i<rows){
    j=0; 
            while(j<count){
            System.out.print(" ");
       j++;
}
j=0;
while(j<=i){
            System.out.print(arr[i][j]+" ");
      j++;
}
System.out.println();
num--;
 i++;
}
  }
}

When the above code is executed, it produces the following result

Output 2

 

Program to display pascal triangle Using a do-while loop

Program 1

This program allows the user to enter the number of rows,  and then the program will display the pascal triangle number pattern using do-while loop in the Java language

//Java programto print pascal triangle using 2D Array
import java.util.Scanner;
//class diclaration
class Disp_Pascal_Triangle_2DArrDoWhile1{
public static void main (String args[]){//main method
Scanner scan=new Scanner(System.in);
//Scanner object for user input
System.out.print("Enter the Number of rows: ");
//Ask input from the user for row
int rows=scan.nextInt();
//read the input from the user
int i=0,j=0,num=rows-1;
//declare and initialize variables
int arr[][]=new int[50][50];
//declare two D array
i=0;
do{
arr[i][i]=arr[i][0]=1;
 i++;
  }while(i<rows);
  i=0; 
   do{
     j=1;
            while(j<i){
             arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
        j++;
}
 i++;
}while(i<rows);
      i=0;       
    do{
    j=0; 
            while(j<num){
            System.out.print(" ");
       j++;
}
j=0;
do{
            System.out.print(arr[i][j]+" ");
      j++;
}while(j<=i);
System.out.println();
num--;
 i++;
}while(i<rows);
  }
}

When the above code is executed, it produces the following result

Output 3

 

Suggested for you

For loop in Java               while loop in Java             Do while loop in Java

Nested for loop in Java     Nested while loop in Java

if statement in Java        Data type and variable in Java  operator in Java

 

Similar post

C program to print pascal triangle

C++ program to print pascal triangle

Java program to triangle number pattern

Java program to triangle number pattern using while loop

Java program to pyramid triangle star pattern

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.