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

 

 

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

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…

2 months 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…

10 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,…

10 months ago