jTruBela

Pascals Triangle Recursion

Apr 21st, 2021 (edited)
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. //********************************************************************************/
  2. //File Name: PascalsTriangle.java                       Author: Justin Trubela    /        
  3. //Section: 03                                                                     /
  4. //                                                                                /
  5. //Purpose:  Write a program using recursion. The program asks the user to input   /
  6. //an integer n and calculates n many rows of Pascals triangle                     /                                                              
  7. //********************************************************************************/
  8. import java.util.Scanner;
  9.  
  10. public class PascalsTriangle {
  11.     public static void main(String[] args) {
  12.        
  13.         Scanner scan = new Scanner(System.in);
  14.        
  15.         //not entirely n-th but its a start based on the string of spaces i'm breaking up soon
  16.         System.out.println("Enter a number from 1 to 9: ");
  17.        
  18.         //scan in the n-th row from user
  19.         int input = scan.nextInt();
  20.         while(input<=0 || input>=10) {
  21.             System.out.println("not in the assigned range");
  22.             System.out.println("Enter a number from 1 to 9: ");
  23.  
  24.            
  25.             input = scan.nextInt();
  26.  
  27.         }
  28.  
  29.        
  30.         System.out.println("Pascals Triangle:");
  31.        
  32.         //Recursive function
  33.         printTriangle(input);
  34.     }
  35.  
  36.  
  37.     public static void printTriangle(int n) {
  38.        
  39.         /*could probably put this is an array and have it iterate n/2 times each row to compensate printed number difference */
  40.         String spaces = "                                "; // sets up the spaces on the left of triangle(32 spaces to be exact)
  41.         int spacesLength = spaces.length();
  42.        
  43.         //iterate through for each row of the triangle
  44.         for (int i = 0; i < n; i++) {
  45.             for (int j = 0; j <= i; j++) {
  46.  
  47.                 System.out.print(" " + pascalsTriangle(i, j) + "  ");  
  48.             }          
  49.             System.out.println();
  50.  
  51.             System.out.print(spaces.substring(0, spacesLength/2));
  52.             /*as it iterates chop off 4 more spaces from the string so that it becomes shorter*/
  53.             spacesLength=spacesLength-4;
  54.         }
  55.     }
  56.  
  57.     public static int pascalsTriangle(int i, int j) {
  58.         if (i==0) /*center the first 1*/{
  59.            
  60.            
  61.             /*same thing here with utilizing n/2 spaces to compensate the difference*/
  62.             System.out.print("                  ");
  63.         }
  64.        
  65.         //
  66.         if (j == 0 || j == i) {
  67.             return 1;
  68.         }
  69.         else {
  70.             return pascalsTriangle(i - 1, j - 1) + pascalsTriangle(i - 1, j);
  71.         }
  72.     }
  73. }
Add Comment
Please, Sign In to add comment