Table of Contents
Java program to print pascal triangle
In this tutorial, we will discuss the Java program to print pascal triangle
In this post, we will learn how to display pascal triangle in java language using for, while and do-while loop
Code to print pascal triangle
Program to print pascal triangle Using for loop
Program 1
This program allows the user to enter the number of rows and it will display pascal triangle number pattern using for loop in Java language
//Java programto print pascal pyramid number pattern import java.util.Scanner; class PascalTriangle{ public static void main (String args[]){ //create object for scanner Scanner scan=new Scanner(System.in); //Enter number ofrows for pascal triangle System.out.print("Enter the row for pascal triangle: "); int rows=scan.nextInt(); int count=1;//variable count initialize as 1 for(int i=0; i<rows; i++){ for(int j=rows; j>i; j--){//inner for loop System.out.print(" "); //first inner loop print space } count=1; for(int k=0; k<=i; k++){ System.out.print(count+" "); count=count*(i-k)/(k+1); //second inner loop prints number according to the given equation } System.out.println(); } } }
When the above code is executed, it produces the following result
Program to print Pascal triangle Using while loop
Program 2
This program allows the user to enter the number of rows and it will be displayed pascal triangle number pattern using while loop in Java language.
//Java programto print pascal pyramid number pattern import java.util.Scanner; class PascalTrianglewhile{ public static void main (String args[]){ Scanner scan=new Scanner(System.in); //create object for scanner System.out.print("Enter the row for pascal triangle: "); //Enter number ofrows for pascal triangle int rows=scan.nextInt(); int count=1; int i=0; while( i<rows){ int j=rows; while(j>i){ System.out.print(" "); //first inner loop print space j--; } count=1; int k=0; while(k<=i){ System.out.print(count+" "); count=count*(i-k)/(k+1); //second inner loop prints number according to the given equation k++; } System.out.println(); i++; } } }
When the above code is executed, it produces the following result
Program to print pascal triangle Using do-while loop
Program 3
This program allows the user to enter the number of rows and it will be displayed pascal triangle number pattern using the do-while loop in Java language.
//Java programto print pascal pyramid number pattern import java.util.Scanner; class PascalTriangleDowhile{ public static void main (String args[]){ Scanner scan=new Scanner(System.in); //create object for scanner System.out.print("Enter the row for pascal triangle: "); //Enter number ofrows for pascal triangle int rows=scan.nextInt(); int count=1; int i=0; do{ int j=rows; do{ System.out.print(" "); //first inner loop print space j--; }while(j>i); count=1; int k=0; do{ System.out.print(count+" "); count=count*(i-k)/(k+1); //second inner loop prints number according to the given equation k++; } while(k<=i); System.out.println(); i++; }while( i<rows); } }
When the above code is executed, it produces the following result
Suggested for you
For loop in Java while loop in Java Do while loop in Java
Nested for loop in Java Nested while loop in Java
if statement in Java Data type and variable in Java operator in Java
Similar post
C program to print pascal triangle
C++ program to print pascal triangle
Java program to triangle number pattern
Java program to triangle number pattern using while loop
Java program to pyramid triangle star pattern