pascal triangle in java using array

Solutions on MaxInterview for pascal triangle in java using array by the best coders in the world

showing results for - "pascal triangle in java using array"
Miranda
26 Oct 2018
1// Pascal triangle in java using array
2import java.util.Scanner;
3public class PascalTriangleUsingArray
4{
5   public static void main(String[] args)
6   {
7      Scanner sc = new Scanner(System.in);
8      int num, a, b, arr[][], p;
9      System.out.println("Please enter number of rows: ");
10      num = sc.nextInt();
11      p = num - 1;
12      arr = new int[num][num];
13      for(a = 0; a < num; a++)
14      {
15         for(b = 0; b <= a; b++)
16            if(b == 0 || b == a)
17               arr[a][b] = 1;
18            else
19               arr[a][b] = arr[a - 1][b - 1] + arr[a - 1][b];
20      }
21      System.out.println("Pascal's triangle: \n");
22      for(a = 0; a < num; a++)
23      {
24         for(b = 0; b <= p; b++)
25            System.out.print(" ");
26         p--;
27         for(b = 0; b <= a; b++)
28            System.out.print(arr[a][b] + " ");
29         System.out.println();
30      }
31      sc.close();
32   }
33}
Josué
29 Feb 2016
1// Pascal triangle program in java without using arrays
2import java.util.Scanner;
3public class PascalTriangleDemo 
4{
5   public static void main(String[] args) 
6   {
7      System.out.println("Please enter number of rows to print pascal's triangle: ");
8      Scanner sc = new Scanner(System.in);
9      int row = sc.nextInt();            
10      System.out.println("Pascal's triangle with " + row + " rows.");
11      displayPascalTriangle(row);
12      sc.close();
13   }
14   public static void displayPascalTriangle(int r) 
15   {
16      for(int a = 0; a < r; a++) 
17      {
18         int num = 1;
19         System.out.printf("%" + (r - a) * 2 + "s", "");
20         for(int b = 0; b <= a; b++) 
21         {
22            System.out.printf("%4d", num);
23            num = num * (a - b) / (b + 1);
24         }
25         System.out.println();
26      }
27   }
28}
Sofie
25 Jun 2016
1/*
2Author: Jeffrey Huang
3*/
4import java.util.*;
5public class PascalTriangleCreator
6{
7    public static long factorial(long n){
8        /*
9        The whole purpose of this method is to find the factorial of a number,
10        since java does not have a built in method for it. Calculating n choose 
11        r is done using factorial, and since this code will be used repeatedly,
12        it is wise to put it in a separate method.
13        */
14        long factorial;
15        if (n==0){
16            factorial=1;
17        }
18        else{
19            factorial=1;
20            for (int counter=1;counter<=n;counter++){
21                factorial=factorial*counter;
22            }
23        }
24        return factorial;
25    }
26    
27    public static long FinalValue(long n, long r){
28        //Calculates n choose r by calling the factorial method.
29        return factorial(n) / ( factorial(n-r) * factorial(r) );
30    }
31    
32 public static void main(String[] args) {
33     Scanner sc=new Scanner (System.in);
34     long rows=1;
35     long i,j;
36     while (rows!=0){
37  System.out.println("How many rows of Pascal's triangle would you like to print? (0 to stop; 1-20 rows)");
38  rows=sc.nextLong();
39  //The following while loop ensures that the user cannot input an invalid number.
40  while (rows<0||rows>20){
41      System.out.println("Invalid input.");
42      System.out.println("How many rows of Pascal's triangle would you like to print? (0 to stop; 1-20 rows)");
43      rows=sc.nextLong();
44  }
45  /*
46  The following if else block makes the code more efficient. Otherwise, if the user 
47  enters zero at any other point than at the start of the loop, the program will go 
48  through the long process of trying to print a triangle before terminating the
49  program. 
50  
51  Using the following method, it is true that rows==0 is tested for twice, but
52  it shortens the execution time immensely. And we know that when zero is true
53  for the if statement, it is guaranteed to be true when breaking the loop.
54  */
55  if (rows==0){
56      System.out.println("Program terminated by user.");
57  }
58  else{
59  for(i = 0; i < rows; i++) {
60      //Iterates through the number of rows required.
61         for(j = 0; j <= rows-i; j++){
62           System.out.print("   ");
63            //Iterates the printing of spaces.
64         }
65         for(j = 0; j <= i; j++){
66           if ((FinalValue(i, j))>9999) {
67             System.out.print(" "); 
68           }
69           else if ((FinalValue(i, j))>999){
70             System.out.print("  "); 
71           }
72           else if ((FinalValue(i, j))>99){
73             System.out.print("   "); 
74           }
75           else if ((FinalValue(i, j))>9){
76             System.out.print("    "); 
77           }
78           else{
79            System.out.print("     "); 
80           }
81            System.out.print(FinalValue(i, j));
82            //Prints a number of spaces plus a number.
83         }
84         System.out.println();
85        }
86        }
87     }
88 sc.close();
89 
90}
91}
Joshua
29 Mar 2020
1import java.util.Scanner;
2class Pascal_Triangle
3{//opening of class
4    public static void main(String args[])
5    {//opening of main
6        Scanner sc=new Scanner(System.in);
7        int n,i,j,a[][];
8        //taking user's input.
9        System.out.println("HOW MANY STEPS?");
10        n=sc.nextInt();
11        a=new int[n][n];
12        //filling the 2D matrix.
13        for(i=0;i<n;i++){
14            for(j=0;j<=i;j++)
15                if(j==0 || j==i)
16                    a[i][j]=1;
17                else
18                    a[i][j]=a[i-1][j-1]+a[i-1][j];
19        }
20        //displaying the Pascal's Triangle as the output.
21        System.out.println("\nOUTPUT:\n");
22        for(i=0;i<n;i++)
23        {
24            for(j=0;j<=i;j++)
25                System.out.print(a[i][j]+"\t");
26
27            System.out.println();
28        }
29    }//clossing of main
30}//closing of class
Luisa
04 Jun 2017
1import java.util.Scanner;
2class Pascal_Triangle
3{//opening of class
4    public static void main(String args[])
5    {//opening of main
6        Scanner sc=new Scanner(System.in);
7        int n,i,j,a[][],s;
8        //taking user's input.
9        System.out.println("HOW MANY STEPS?");
10        n=sc.nextInt();
11        s=n-1; 
12        a=new int[n][n];
13        //filling the 2D matrix.
14        for(i=0;i<n;i++){
15            for(j=0;j<=i;j++)
16                if(j==0 || j==i)
17                    a[i][j]=1;
18                else
19                    a[i][j]=a[i-1][j-1]+a[i-1][j];
20        }
21        //displaying the Pascal's Triangle as the output.
22        System.out.println("\nOUTPUT:\n");
23        for(i=0;i<n;i++)
24        {
25            for(j=0;j<=s;j++)
26            System.out.print(" ");//printing blank spaces at the beginning of rows
27            
28            s--;
29            
30            for(j=0;j<=i;j++)
31                System.out.print(a[i][j]+" ");
32
33            System.out.println();
34        }
35    }//clossing of main
36}//closing of class
queries leading to this page
all pascal 27s triangle pattern javawrite a java program to display pascal 27s trianglepascal 27s triangle pythonpascal 27s triangle java programright pascal triangle number in javaprint pascal 27s trianglepascal traingle java coe easy pascal 27s triangle stringpascal triangle print triangle javapascal trianglegfghow to make a pascal triangle in javapascal 27s pyramid javasubarray solve pascal triangle recursion java pascal triangle array javapascal e2 80 99s triangle program simple code in javapascals triangle codehow to program pascals trianglehow to return one dimensional array of pascal traingle javajava pascal triangle programprogram for pascal triangle in javapascal traingle javapascal triangle program in javause ondd array in pascal triangle javapython program to create a function that prints pascal 27s triangle pascal 27s triangle solution javapascal 27s triangle in javapascal trianglepascal triangle java codepascal triangle in java using arrayhow to make pascal e2 80 99s triangle in javapascal triangle geeksforgeekspascal 27s triangle code in javaprint pascals triangleright pascal triangle in javapascal traingle code geeks for geekspascal triangle java programjava pascal trianglepascal 27s triangle codegenerate pascal 27s triangle javapascal triangle javaprint pascals triangle in javapascal triangle logic javapascal triangle codehow to print pascal 27s triangle in javapascal tree javapascal triangle from 0 in javapascal 27s triangle javaallintitle 3apascal triangle program in java without using arrayspascal e2 80 99s triangle program in javaleft pascal triangle in javajava program to display pascal triangleuse array in pascal triangle javapascal triangle c 2b 2bpascal triangle pattern in javapascal triangle in javapascal 27s triangle program in javaprint pascal triangleprint pascal e2 80 99s triangle in javaprint pascal triangle in javaprint pascal 27s triangle in javapascal diamond in javaprinting pascal triangle in javapascal triangle program in java without using arrayshow to print pascal triangle in javapascals triangle javarow of pascal 27s triangle in javapascal algorith in javahow to create pascal 27s triangle in javawap to design pascal triangle in javapascal number diamond in javapascals triangle in javapascal triangle pattern program in javapascals triangle 2d array apppascal triangle formula javaallintitle 3apascal triangle in java using arraypascal algorithm javapascal triangle in java programa java program to display pascal 27s triangle pascal triangle geekspascal 27s triangle 2d array c 2b 2bhow to make pascal 27s triangle in javahow to solve pascal triangle in javapascal triangle using 2a in javapascal triangle using arraypascal 27s triangle java codepascal triangle left in javapascal triangle code in javapascal 27s triangle in a 2d matrix c 2b 2bpascal triangle in java using array