Array

Java code to generate pascal triangle using arrays with user input

Java code to generate pascal triangle using arrays with user input

In this tutorial, we will discuss the concept of the Java code to generate a pascal triangle using arrays with user input

In this topic, we are going to learn how  to write a program to print Pascal triangle patterns using numbers using user input 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 is asked to enter the number of rows and then it will show a pascal triangle number pattern using for loop in the Java language

Program 1

//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);
//Scanner object for user input
System.out.print("Enter the row for pascal triangle: ");
//Ask input from the user for row
int row=scan.nextInt();//read the input from the user
int i,j,temp=1,counter=0;//declare variables
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++)//outer for loop
{
    for(j=(row-1); j>i; j--)//inner for loop
        System.out.print(" ");//print initial space
    for(j=0; j<=i; j++)//inner for loop
    {
        if(i==0)
        System.out.print("1");
    else
    { if(j==0|| j==i)
        System.out.print("1 ");
    else{
        arrayTemp[temp]=array[counter]+array[counter+1];
        System.out.print(arrayTemp[temp]+" ");
        //print elemenys
        temp++;
        counter++;
    }
    }
    }
    System.out.println();//mpve to next line
    arrayTemp[temp]=1;
    if(i>1)
    {
        counter=0;
        array[counter]=1;
        for(temp=1,counter=1; counter<=i; temp++, counter++)
            array[counter]=arrayTemp[temp];
        temp=1;
        counter=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 is asked to enter the number of rows and then it will show a pascal triangle number pattern using the while loop in the Java language

Program 2

//Java programto print pascal triangle using Array
import java.util.Scanner;
//class diclaration
class Disp_PascalTriangleArraywhile{
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 rows=scan.nextInt();//read the input from the user
int row=5,i,j,temp=1,count=0;
int[] array=new int[rows];//declare single dim array
int[] arrayTemp=new int[rows];//declare single dim temp array
array[0]=1;
array[1]=1;

i=0; 
while(i<rows)
{
    j=(rows-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 2

 

Java Code to display pascal triangle using do-while loop

In this program, the user is asked to enter the number of rows and then it will show a pascal triangle number pattern using the do-while loop in the Java language

Program 3

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

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

Java program to print pascal triangle using an array

Java program to print pascal triangle using array using user input

 

 

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

Java code to Pascal triangle pattern using Array
C program to Generate Pascal triangle using 1 D array
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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago