1

So far I have this, but I am not quite sure how printPTriangle can print the triangle using the code. If someone could help me out with this, I would be grateful.

public static int factorial(int n) {
    if (n == 1) {
        return 1;
    }
    return n * (factorial(n - 1));
}

public static int pascalsNumber(int x, int y) {
    return factorial(x)/(factorial(y) * factorial((x - y))); //Using combinations formula
}

public static void printPTriangle(int z) {



}
XxRUSxX
  • 15
  • 1
  • 5

4 Answers4

1

Try this,

public class PascalTriangle {

public static void main(String[] args) {

    int rows = 10;


    for(int i =0;i<rows;i++) {
        int number = 1;
        System.out.format("%"+(rows-i)*2+"s","");
        for(int j=0;j<=i;j++) {
             System.out.format("%4d",number);
             number = number * (i - j) / (j + 1);

        }
        System.out.println();
    }

}
}

Note the formatting commands used above to create a nicely formatted triangle. %4d instructs the formatter to print the number within 4 spaces.

ajitksharma
  • 4,523
  • 2
  • 21
  • 40
1

I too am a beginner at Java and am working on Pascals triangle. I liked the formatting indicated above and introduced it to my code. I made the spacing a little larger to account for a bigger triangle.

import java.util.Scanner;

public class Pascal {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the row number up to which Pascal's triangle has to be printed: ");
        int row = scanner.nextInt();
        print(row);

        scanner.close();
    }

    public static void print(int n) {
        for (int i = 0; i < n; i++) {
            System.out.format("%"+(n-i)*3+"s","");
            for (int j = 0; j <= i; j++) {
                System.out.format("%6d",(pascal(i, j)));
            }
            System.out.println();
        }
    }

    public static int pascal(int i, int j) {
        if (j == 0) {
            return 1;
        } else if (j == i) {
            return 1;
        } else {
            return pascal(i - 1, j - 1) + pascal(i - 1, j);
        }

    }

}
Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
Kosta
  • 11
  • 3
0

Here is the solution:

public static void main(String[] args) 
{
    pascal(1,10);
}
static int pascal(int start, int end)
{
    if(start>=end)
        return 0;
    int number = 1;
    System.out.format("%"+(end-start)*2+"s","");
    pascal2(start,number,0);
    System.out.println();
    return pascal(start+1,end);
}
static int pascal2(int start,int number,int end)
{
    if(end>start)
        return 1;
    System.out.format("%4d",number);
    return pascal2(start,number * (start - end) / (end + 1),end+1);
}
hakar
  • 1
  • 1
0

I was just stumped on this programming challenge for my Java class, and could not find help anywhere that only uses recursion with no loops. So after hours of pain here it is. All it needs is a main method or demo class to create an instance of this PascalsTriangle class and initialize it with the number of rows.

public class PascalsTriangle {

private StringBuilder str; // StringBuilder to display triangle

/**
 * Starts the process of printing the Pascals Triangle
 * @param rows Number of rows to print
 */
public PascalsTriangle(int rows) {

    str = new StringBuilder();

    printTriangle(rows, str);
}

/**
 * Uses recursion to function as an "outer loop" and calls
 * itself once for each row in triangle. Then displays the result
 * @param row The number of the row to generate
 * @param str StringBuilder to insert each row into
 */
public static void printTriangle(int row, StringBuilder str) {

    // calls itself until row equals -1
    if (row >= 0) {

        // calls lower function to generate row and inserts the result into front of StringBuilder
        str.insert(0, getRow(row, 0) + "\n");

        // calls itself with a decremented row number
        printTriangle(row - 1, str);
    } else {

        // when the base case is reached - display the result
        JOptionPane.showMessageDialog(null, str);
        System.exit(0);
    }
}

/**
 * Uses recursion to act as the "inner loop" and calculate each number in the given row
 * @param rowNumber Number of the row being generated
 * @param elementNumber Number of the element within the row (always starts with 0)
 * @return String containing full row of numbers or empty string when base case is reached
 */
public static String getRow(int rowNumber, int elementNumber) {

    // calls itself until elementNumber is greater than rowNumber
    if (elementNumber <= rowNumber) {

        // calculates element using combinations formula: n!/r!(n-r)!
        int element = fact(rowNumber) / (fact(elementNumber) * (fact(rowNumber - elementNumber)));

        // calls itself for each element in row and returns full String            
        return element + " " + getRow(rowNumber, elementNumber + 1);

    } else return "";
}

/**
 * Helper function that uses recursion to calculate factorial of given integer
 * @param n Number to calculate factorial
 * @return Factorial
 */
public static int fact(int n) {
    if (n <= 0)
        return 1;
    else
        return n * fact(n - 1);
}
austinw
  • 594
  • 1
  • 7
  • 18