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 declares and initializes integer variables, it will display a pascal triangle number pattern usingfor loop in the Java language according to the rows
Program 1
//Java programto print pascal triangle using 2D Array
import java.util.Scanner;
//class diclaration
class Disp_Pascal_Triangle_2DArr{
public static void main (String args[]){//main method
Scanner scan=new Scanner(System.in);
//Scanner object for user input
System.out.print("Enter the Number of rows: ");
//Ask input from the user for row
int row=scan.nextInt();//read the input from the user
int i=0,j=0,count=row-1;//declare variables
int arr[][]=new int[50][50];//declare two D array
for(i=0; i
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 declares and initializes integer variables, it will display a pascal triangle number pattern using the while loop in the Java language according to the rows
Program 2
//Java programto print pascal triangle using 2D Array
import java.util.Scanner;
//class diclaration
class Disp_Pascal_Triangle_2DArrWhil{
public static void main (String args[]){//main method
Scanner scan=new Scanner(System.in);
//Scanner object for user input
System.out.print("Enter the Number of rows: ");
//Ask input from the user for row
int row=scan.nextInt();//read the input from the user
int i=0,j=0,count=row-1;//declare variables
int arr[][]=new int[50][50];//declare two D array
i=0;
while(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 declares and initializes integer variables, it will display a pascal triangle number pattern using the do-while loop in the Java language according to the rows
Program 3
//Java programto print pascal triangle using 2D Array
import java.util.Scanner;
//class diclaration
class Disp_Pascal_Triangle_2DArrDoWhile{
public static void main (String args[]){//main method
Scanner scan=new Scanner(System.in);
//Scanner object for user input
System.out.print("Enter the Number of rows: ");
//Ask input from the user for row
int row=scan.nextInt();//read the input from the user
int i=0,j=0,count=row-1;//declare variables
int arr[][]=new int[50][50];//declare two D array
i=0;
do{
arr[i][i]=arr[i][0]=1;
i++;
}while(i
When the above code is executed, it produces the following result