Q:

pascals triangle java

/*
Author: Jeffrey Huang
*/
import java.util.*;
public class PascalTriangleCreator
{
    public static long factorial(long n){
        /*
        The whole purpose of this method is to find the factorial of a number,
        since java does not have a built in method for it. Calculating n choose 
        r is done using factorial, and since this code will be used repeatedly,
        it is wise to put it in a separate method.
        */
        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){
        //Calculates n choose r by calling the factorial method.
        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();
  //The following while loop ensures that the user cannot input an invalid number.
  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();
  }
  /*
  The following if else block makes the code more efficient. Otherwise, if the user 
  enters zero at any other point than at the start of the loop, the program will go 
  through the long process of trying to print a triangle before terminating the
  program. 
  
  Using the following method, it is true that rows==0 is tested for twice, but
  it shortens the execution time immensely. And we know that when zero is true
  for the if statement, it is guaranteed to be true when breaking the loop.
  */
  if (rows==0){
      System.out.println("Program terminated by user.");
  }
  else{
  for(i = 0; i < rows; i++) {
      //Iterates through the number of rows required.
         for(j = 0; j <= rows-i; j++){
           System.out.print("   ");
            //Iterates the printing of spaces.
         }
         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));
            //Prints a number of spaces plus a number.
         }
         System.out.println();
        }
        }
     }
 sc.close();
 
}
}
1
//  C++ code for Pascal's Triangle 
#include <stdio.h> 
  
// See https://www.geeksforgeeks.org/space-and-time-efficient-binomial-coefficient/  
// for details of this function 
int binomialCoeff(int n, int k); 
  
// Function to print first 
// n lines of Pascal's  
// Triangle 
void printPascal(int n) 
{ 
    // Iterate through every line and 
    // print entries in it 
    for (int line = 0; line < n; line++) 
    { 
        // Every line has number of  
        // integers equal to line  
        // number 
        for (int i = 0; i <= line; i++) 
            printf("%d ", 
                    binomialCoeff(line, i)); 
        printf("\n"); 
    } 
} 
  
// See https://www.geeksforgeeks.org/space-and-time-efficient-binomial-coefficient/ 
// for details of this function 
int binomialCoeff(int n, int k) 
{ 
    int res = 1; 
    if (k > n - k) 
    k = n - k; 
    for (int i = 0; i < k; ++i) 
    { 
        res *= (n - i); 
        res /= (i + 1); 
    } 
      
    return res; 
} 
  
// Driver program  
int main() 
{ 
    int n = 7; 
    printPascal(n); 
    return 0; 
} 
0

New to Communities?

Join the community