Number pattern

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

Print Pascal Triangle

Display the pascal triangle in Java using loops

Pascal Triangle in Java

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

Output 1

 

 

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

Output 2

 

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

 

Output 3

 

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

Alphabet  pattern in C++ language

Alphabet triangle pattern in C++ language using while loop

Pascal Triangle program in C++ language
Java code to generate pascal triangle using arrays with user input
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago