pascal triangle in java using array code example
Example 1: Pascal triangle in java using array
import java.util.Scanner;
public class PascalTriangleUsingArray
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num, a, b, arr[][], p;
System.out.println("Please enter number of rows: ");
num = sc.nextInt();
p = num - 1;
arr = new int[num][num];
for(a = 0; a < num; a++)
{
for(b = 0; b <= a; b++)
if(b == 0 || b == a)
arr[a][b] = 1;
else
arr[a][b] = arr[a - 1][b - 1] + arr[a - 1][b];
}
System.out.println("Pascal's triangle: \n");
for(a = 0; a < num; a++)
{
for(b = 0; b <= p; b++)
System.out.print(" ");
p--;
for(b = 0; b <= a; b++)
System.out.print(arr[a][b] + " ");
System.out.println();
}
sc.close();
}
}
Example 2: Pascal triangle program in java without using arrays
import java.util.Scanner;
public class PascalTriangleDemo
{
public static void main(String[] args)
{
System.out.println("Please enter number of rows to print pascal's triangle: ");
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
System.out.println("Pascal's triangle with " + row + " rows.");
displayPascalTriangle(row);
sc.close();
}
public static void displayPascalTriangle(int r)
{
for(int a = 0; a < r; a++)
{
int num = 1;
System.out.printf("%" + (r - a) * 2 + "s", "");
for(int b = 0; b <= a; b++)
{
System.out.printf("%4d", num);
num = num * (a - b) / (b + 1);
}
System.out.println();
}
}
}
Example 3: pascals triangle java
import java.util.*;
public class PascalTriangleCreator
{
public static long factorial(long n){
long factorial;
if (n==0){
factorial=1;
}
else{
factorial=1;
for (int counter=1;counter<=n;counter++){
factorial=factorial*counter;
}
}
return factorial;
}
public static long FinalValue(long n, long r){
return factorial(n) / ( factorial(n-r) * factorial(r) );
}
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
long rows=1;
long i,j;
while (rows!=0){
System.out.println("How many rows of Pascal's triangle would you like to print? (0 to stop; 1-20 rows)");
rows=sc.nextLong();
while (rows<0||rows>20){
System.out.println("Invalid input.");
System.out.println("How many rows of Pascal's triangle would you like to print? (0 to stop; 1-20 rows)");
rows=sc.nextLong();
}
if (rows==0){
System.out.println("Program terminated by user.");
}
else{
for(i = 0; i < rows; i++) {
for(j = 0; j <= rows-i; j++){
System.out.print(" ");
}
for(j = 0; j <= i; j++){
if ((FinalValue(i, j))>9999) {
System.out.print(" ");
}
else if ((FinalValue(i, j))>999){
System.out.print(" ");
}
else if ((FinalValue(i, j))>99){
System.out.print(" ");
}
else if ((FinalValue(i, j))>9){
System.out.print(" ");
}
else{
System.out.print(" ");
}
System.out.print(FinalValue(i, j));
}
System.out.println();
}
}
}
sc.close();
}
}
Example 4: pascal's triangle java 2d array
import java.util.Scanner;
class Pascal_Triangle
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,i,j,a[][],s;
System.out.println("HOW MANY STEPS?");
n=sc.nextInt();
s=n-1;
a=new int[n][n];
for(i=0;i<n;i++){
for(j=0;j<=i;j++)
if(j==0 || j==i)
a[i][j]=1;
else
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
System.out.println("\nOUTPUT:\n");
for(i=0;i<n;i++)
{
for(j=0;j<=s;j++)
System.out.print(" ");
s--;
for(j=0;j<=i;j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
}
Example 5: pascal's triangle java 2d array
import java.util.Scanner;
class Pascal_Triangle
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,i,j,a[][];
System.out.println("HOW MANY STEPS?");
n=sc.nextInt();
a=new int[n][n];
for(i=0;i<n;i++){
for(j=0;j<=i;j++)
if(j==0 || j==i)
a[i][j]=1;
else
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
System.out.println("\nOUTPUT:\n");
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
}
}