Number pattern

Program to print the Pascal Triangle in Java

Program to print the Pascal Triangle pattern in Java

In this tutorial, we will discuss the concept of the Program to print the Pascal Triangle pattern in Java

In this topic, we are going to learn how  to write a program to print the Pascal triangle patterns using numbers in the Java programming language

Here, we  use for, while, and do-while loops for printing pascal triangle

Print Pascal Triangle

Display 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 display a pascal triangle number pattern using for loop in the Java language

Program 1

//Java programto print pascal triangle
//importing input output packeges
import java.util.Scanner;
//main class
class Disp_PascalTriangle{
public static void main (String args[]){//main method
Scanner scan=new Scanner(System.in);
System.out.print("Enter the row for pascal triangle: ");
int rows=scan.nextInt();
//input no of rows for print pascal triangle
int count=1;
for(int i=0; i<rows; i++){
   for(int j=rows; j>i; j--){
   System.out.print(" ");
   //print white spaces for left side
   }
   count=1;
   for(int k=0; k<=i; k++){
   System.out.print(count+" ");
     count=count*(i-k)/(k+1);
    
   }
   System.out.println();
   //move to next line

}
}
}

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

Print Pascal Triangle

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 display the pascal triangle number pattern using the while loop in the Java language

Program 2

//Java programto print pascal triangle
//importing input output packeges
import java.util.Scanner;
//main class
class Disp_PascalTriWhile{
public static void main (String args[]){//main method
Scanner scan=new Scanner(System.in);
System.out.print("Enter the row for pascal triangle: ");
int rows=scan.nextInt();
//input no of rows for print pascal triangle
int count=1;
int i=0; 
while(i<rows){
    int j=rows;
   while(j>i){
   System.out.print(" ");
   //print white spaces for left side
   j--;
   }
   count=1;
   int k=0;
   while(k<=i){
   System.out.print(count+" ");
     count=count*(i-k)/(k+1);
    k++;
   }
   System.out.println();
   //move to next line
i++;
}
}
}

 

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

Pascal Triangle in Java

 

Java Code to display the pascal triangle using the do-while loop

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

Program 3

//Java programto print pascal triangle
//importing input output packeges
import java.util.Scanner;
//main class
class Disp_PascalTriDoWhile{
public static void main (String args[]){//main method
Scanner scan=new Scanner(System.in);
System.out.print("Enter the row for pascal triangle: ");
int rows=scan.nextInt();
//input no of rows for print pascal triangle
int count=1;
int i=0; 
do{
    int j=rows;
   do{
   System.out.print(" ");
   //print white spaces for left side
   j--;
   }while(j>i);
   count=1;
   int k=0;
   do{
   System.out.print(count+" ");
     count=count*(i-k)/(k+1);
    k++;
   }while(k<=i);
   System.out.println();
   //move to next line
i++;
}while(i<rows);
}
}

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

Print Pascal Triangle

 

 

Suggested for you

Data type and variable in Java language

The operator in the Java language

for loop in Java language

While loop in Java language

 

 

Similar post

Java code to print pascal triangle

C code to print pascal triangle

C++ code to print pascal triangle

Java code to print pascal triangle using an array

Java program to print pascal triangle using array using user input

C program to print a pascal triangle using an array

C program to print pascal triangle using array using user input

Java program to pascal triangle number pattern using 2 D array

C program to pascal triangle number pattern using 2 D array

 

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

 

Program to display combined Pyramid pattern in C using while loop
C Program to print Pascal Triangle
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

Explanation of one dimensional array (Data structure)

Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…

2 days ago

Python program to calculate the sum of odd and even numbers in a list

Python program to calculate the sum of odd and even numbers in a list In…

5 days ago

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

1 week ago

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 weeks ago

C# inverted full pyramid star pattern

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

1 month ago

C# Full Pyramid star pattern program

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

2 months ago