Array

How to write pascal triangle code in Java using arrays

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

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

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

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

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

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

 

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

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

 

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.