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; ii; 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(ii){
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
When the above code is executed, it produces the following result