Program to print the Pascal Triangle in Java
- Home
- Number pattern
- Program to print the Pascal Triangle in Java
- On
- By
- 0 Comment
- Categories: Number pattern, pyramid triangle
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
Display pascal triangle in Java using loops
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
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
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
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