Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
C Sharp Exercise: print Pascal triangle pattern

Pascal’s triangle in Java using 2D Array

Posted on October 3, 2022

Table of Contents

  • Pascal’s triangle in Java using 2D Array
    • Java program to print pascal pattern using 2 D Array
      • Program to display pascal triangle Using for loop
      • Program to display pascal triangle Using while loop
      • Program to display pascal triangle Using a do-while loop

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's triangle in Java uaing 2D Array
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

Pascal's triangle in Java uaing 2D Array
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

Pascal's triangle in Java uaing 2D Array
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

Pascal's triangle in Java uaing 2D Array
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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes