//Pascal's Triangle import java.io.*; /** * This program uses several loops and recursion to create * a pyramid of factorial numbers. The size of the pyramid * is based on a user suplied number */ public class PascalTriangle { /** * The constructor - gets the number of rows from method * getNumberOfRows() and then passes it to drawTriangle() */ public PascalTriangle () throws IOException { int rows = 0; rows = getNumRows(); drawTriangle(rows); } /** * Reads the number in as a String and * passes it to a method where it is turned into * a long. * Then it passes the row number to draw triangle * Since java does not have simplifed input and output * We must create out own method to read in * information, the InputStreamReader and BufferedReader */ public int getNumRows() throws IOException{ InputStreamReader isr = new InputStreamReader(System.in); BufferedReader bis = new BufferedReader(isr); String number = ""; String name; System.out.println("Pascal's Triangle, by Garth Bruen"); System.out.println(); System.out.print("Please enter your name: "); name = bis.readLine(); System.out.print("Please enter a row number between 1 and 21: "); number = bis.readLine(); System.out.println(); System.out.println(name + " ,you have requested " + number + " rows."); System.out.println(); System.out.println("Here's " +name+ "'s triangle!!!"); System.out.println(); return (convert(number)); } /** * Takes a String and returns an int - hence the * "throws NumberFormatException" in the method header. * CODE TO BE IMPLEMENTED. */ public int convert(String num) throws NumberFormatException{ int convertedNum = 0; convertedNum = Integer.parseInt(num); if(convertedNum > 21){ System.out.println(); System.out.print("The number is too large and will "); System.out.println("not print properly"); System.out.println("Please run again and re-read instructions"); System.out.println(); convertedNum = 0; return convertedNum; //My invention, an error message which //does not print the huge triangle and ends the program } return convertedNum; } /** * Returns the factorial of a given number using recursion * CODE TO BE IMPLEMENTED */ public long getFactorial(long fact) { long factorialResult = 1; if(fact == 0 || fact == 1) { return factorialResult;} else { factorialResult = fact*getFactorial(--fact); return factorialResult; } } /** * Returns the number that goes into each slot of * Pascal's triangle. * @param r the number of the row * @param n the nth element of that row. */ public long getPascalNum(int n, int r) { long pascalNum = 0; pascalNum = (getFactorial(n)/(getFactorial(r)*(getFactorial(n-r)))); return pascalNum; } /** * The triangle gets drawn in this method. * @param rows how many rows to pass in */ public void drawTriangle(int rows) { for(int i = 0; i < rows; i++) { for(int j = (rows - i); j > 0; j--){ System.out.print(" "); } for(int h = 0; h <= i; h++){ System.out.print(getPascalNum(i, h) + " "); } System.out.println(); } } public static void main (String args[]) throws IOException { PascalTriangle pt = new PascalTriangle(); } }