java program to display pascal triangle

Solutions on MaxInterview for java program to display pascal triangle by the best coders in the world

showing results for - "java program to display pascal triangle"
Moritz
27 Mar 2016
1#include <stdio.h>
2int main() {
3   int rows, coef = 1, space, i, j;
4   printf("Enter the number of rows: ");
5   scanf("%d", &rows);
6   for (i = 0; i < rows; i++) {
7      for (space = 1; space <= rows - i; space++)
8         printf("  ");
9      for (j = 0; j <= i; j++) {
10         if (j == 0 || i == 0)
11            coef = 1;
12         else
13            coef = coef * (i - j + 1) / j;
14         printf("%4d", coef);
15      }
16      printf("\n");
17   }
18   return 0;
19}
20
Lia
06 Jan 2018
1import java.util.Scanner;
2public class PascalsTriangleJava 
3{
4   static int findFactorial(int number)
5   {
6      int factorial;
7      for(factorial = 1; number > 1; number--)
8      {
9         factorial *= number;
10      }
11      return factorial;
12   }
13   // here's the function to display pascal's triangle
14   static int printPascalTraingle(int num, int p) 
15   {
16      return findFactorial(num) / (findFactorial(num - p) * findFactorial(p));
17   }
18   public static void main(String[] args) 
19   {
20      int row, a, b;
21      System.out.println("Please enter number of rows: ");
22      Scanner sc = new Scanner(System.in);
23      row = sc.nextInt();
24      System.out.println("Here's is pascal's triangle: ");
25      for(a = 0; a < row; a++) 
26      {
27         for(b = 0; b < row - a; b++)
28         {
29            System.out.print(" ");
30         }
31         for(b = 0; b <= a; b++)
32         {
33            System.out.print(" " + printPascalTraingle(a, b));
34         }
35         System.out.println();
36      }
37      sc.close();
38   }
39}
Cristina
01 May 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}
queries leading to this page
pascal triangle codepascal triangle using ctriange cpascal e2 80 99s triangle program simple code in javaleft pascal triangle in javaprint pascal triangle in cpppascal traingle java coe easy how to print a pascal s triangle in chow to print pascal triangle in c 2b 2bpascal 27s triangle iic pascals trianglehow to print a pyramid in chow to make pascal 27s triangle in javajava pascal trianglefastest way to print pascals triangle c 2b 2bjava program to print pascal trianglepascal triangle program in javagiven an integer n 2c print the corresponding inverted full pyramid pattern for n for example if n 3d 5 then pattern will be like 3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a pascal triangle in java programpascal triangle using 2a in javaprint pascals triangle schemepython program to create a function that prints pascal 27s triangle pascal triangle left in javac print pascals trianglepascal triangle java programpascal 27s triangle java programprinting patterns in cpascal tree javawrite a program to print pascal triangle inchow to print pascal triangle in javapascal 27s triangle pythonwrite a function which prints the pascal 27s triangle as shown below 3apascal e2 80 99s triangle program in javawrite a program to print pascal triangle pascal triangle geeksprinting pascal triangle in javahow to make pascal 27s triangle using cpascal 27s triangle javaprint pascal triangle gfg practicetriangel logic in cpascal traingle code geeks for geekspyramid structure by using cwrite ac program to display pascal 27s trianglepascal 27s triangletriangle javaprint pascal e2 80 99s triangle in javaprint pascal triangle in schemejava pascal triangle programpascal 27s triangle code in javahow to solve pascal triangle in javahow to create pascal 27s triangle in javapascal trianglepascal triangle c codeprintf triangle a in cpattern program in cprogram to print the pascal triangle in cpppascal triangle java codepascal triangle from 0 in javaprint a pascal 27s trianglerow of pascal 27s triangle in javawrite a program to print pascal 27s triangleprogram to print pascal triangleprint triangles in chow to print right triangle using cwrite a program to print pascal triangle in cpascal 27s triangle codepascals triangle in javaright pascal triangle in javac program to print pascal 27s trianglec programs pyramidhow to print pascal triangle in cprint rom pyramidpascal algorith in javac program for pyramid of starshow to make a pascal triangle in javawap to design pascal triangle in javapascal triangle c pascal triangle code in javapascal 27s triangle java codepascal triangle pattern program in javapascal number diamond in javahow to make pascal e2 80 99s triangle in javaprint pascal triangle in cprint pascal trianglepascal triangle geeksforgeekspatterns in cpascal algorithm javapattern design in cpascal traingle javapascal triangle c 2b 2bpascals triangle javapascal 27s pyramid javapascal triangle in cprint pascal 27s trianglepascal 27s triangle program in javapascal triangle pattern in cpascal trianglegfgprint pascal triangle in c 2b 2ba java program to display pascal 27s triangle print pascals trianglepascals triangle codepascal triangle in java using arrayall pascal 27s triangle pattern javapascal triangle formula javahow to make pascal triangle in c using while loopprintf triangle in chow to print triangle using cc code for floyed pyramidpascal triangle c programpascal 27s triangle in chow to print pascal 27s triangle in javapascal triangle pattern in javasubarray solve pascal triangle recursion java right pascal triangle number in javahow to code pascal triangle in cprint pascal 27s triangle in javahow to print ascals triangelein cfull inverted pyramid using 2a in cpascal triangle javawrite a program to print pascal 27s triangle for a given number of rows print pascal triangle in javaprint pascals triangle in javawrite a java program to display pascal 27s trianglepascal 27s triangle in javahow to program pascals trianglepascal triangle program in cprogram for pascal triangle in javapascal triangle print write a c program to print pascal trianglehow to program pascal 27s triangletriangle programs in cprogram to find the total of pascal triangle in cwrite a c program to print pascal triangle for a given numberpyramid printing in cprint pascal 27s triangle schemepascal diamond in javajava code to print pascal 27s trianglecode for pascal triangleprint pascal number trianglepattern printing in cprocedural loop pyramudpascal triangle logic javaprinting patternpascal triangle in javagenerate pascal 27s triangle javahow to make pascal triangle in c using whiulejava program to display pascal triangletriangle in cpascal 27s triangle solution javapascal 27s triangle stringjava program to display pascal triangle