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

How to write pascal triangle code in Java using arrays

Posted on September 26, 2022

Table of Contents

  • How to write pascal triangle code in Java using arrays
    • Java program to print pascal triangle using Array
      • Program to display pascal triangle Using for loop
      • Program to display pascal triangle Using for loop with user input
      • Program to display pascal triangle Using while loop
      • Program to display pascal triangle Using while loop with user input
      • Program to display pascal triangle Using do-while loop
      • Program to display pascal triangle Using do-while loop with user input

How to write pascal triangle code in Java using arrays

In this tutorial, we will discuss the Title of How to write pascal triangle code in Java using arrays

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

How to write pascal triangle code in Java using arrays
Pascal triangle

Java program to print pascal triangle using Array

Program to display pascal triangle Using for loop

Program 1

In this program, the user declares and initializes some variables as integers, and then the program will display the pascal triangle number pattern using for loop in the Java language

//Java programto print pascal triangle using Array
//class diclaration
class Disp_Pascal_Triangle_Arr{
public static void main (String args[]){//main method
int row=6,i,j,temp=1,count=0;
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++)
{
  for(j=(row-1); j>i; j--)
    System.out.print(" ");//print initial space
  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]+" ");
    //print elemenys
    temp++;
    count++;
  }
  }
  }
  System.out.println();//mpve to next line
  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

How to write pascal triangle code in Java using arrays
Output 1

 

Program to display pascal triangle Using for loop with user input

Program 2

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

//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);
System.out.print("Enter the row for pascal triangle: ");
int row=scan.nextInt();
int i,j,temp=1,count=0;
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++)
{
  for(j=(row-1); j>i; j--)
    System.out.print(" ");//print initial space
  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]+" ");
    //print elemenys
    temp++;
    count++;
  }
  }
  }
  System.out.println();//mpve to next line
  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

How to write pascal triangle code in Java using arrays
pascal triangle

 

Program to display pascal triangle Using while loop

In this program, the user declares and initializes some variables as integers, and then the program will display the pascal triangle number pattern using while loop in Java language

Program 3

//Java programto print pascal triangle using Array
//class diclaration
class Disp_Pascal_Triangle_Array_while1{
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; 
while(i<row_No)
{
  j=(row_No-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

How to write pascal triangle code in Java using arrays
Output 3

 

 

 

Program to display pascal triangle Using while loop with user input

Program 4

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

//Java programto print pascal triangle using Array
import java.util.Scanner;
//class diclaration
class Disp_Pascal_Triangle_Array_while{
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_No=scan.nextInt();//read the input from the user
int 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; 
while(i<row_No)
{
  j=(row_No-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

How to write pascal triangle code in Java using arrays
output 4

 

Program to display pascal triangle Using do-while loop

In this program, the user declares and initializes some variables as integers, and then the program will display the pascal triangle number pattern using do-while loop in Java language

Program 5

//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=4,i,j,temp=1,count=0;
int[] arr=new int[row_No];
//declare single dim array
int[] arrTemp=new int[row_No];
arr[0]=1;
arr[1]=1;
//initialize array elements
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{
    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<row_No);
}
  
}

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

 

How to write pascal triangle code in Java using arrays
Output 5

 

Program to display pascal triangle Using do-while loop with user input

Program 6

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

//Java programto print pascal triangle using Array
import java.util.Scanner;
//class diclaration
class Disp_Pascal_Triangle_Array_Dowhile1{
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_No=scan.nextInt();//read the input from the user
int i,j,temp=1,count=0;
int[] arr=new int[row_No];
//declare single dim array
int[] arrTemp=new int[row_No];
arr[0]=1;
arr[1]=1;
//initialize array elements
i=0; 
do{//outer do-while loop
  j=(row_No-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<row_No);
}
  
}

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

How to write pascal triangle code in Java using arrays
Output 6

 

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