Table of Contents
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
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 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 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
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.