import corejava.*;
/**
This program consists of a class called "white1" for a Java application that
will generate and display Pascal's Triangle.
The user will be prompted for the number of lines of Pascal's Triangle
to display (maximum 30 lines).
Pascal's Triangle will be displayed correctly aligned on the screen.
User will be prompted to display another triangle or exit program.
@version 1..1 20 Sept 1999
@author Kerry-Ann White
*/
public class white1
{
/**
Calculates the factorial of the number coming in
@param n_factorial, an integer to perform the operation on
@return the factorial of the number to the combination method
*/
public static long factorial(int n_factorial)
{ //n_factorial is variable to do overation onif (n_factorial==0) return 1;
return(n_factorial*factorial(n_factorial-1));}
/**
Calculates the coefficient for the triange using the factorial function
and simple division.
@param nn the number to choose from in the set
@param rr the amount that is chosen from nn
@return the coeffiecient of the triangle to the main method
*/
public static long combination(int nn, int rr)
{ //nn the number to choose from in the set//rr the amount that is chosen from nn
long numerator = (factorial(nn));
long denominator = (factorial(nn-rr))*(factorial(rr));
return numerator/denominator;}
/**
Reads in first value for the number of lines from the user.
Test to see if number between 0 and 30, excluding 0
If so do the following, but if not exit with message.
Calls combination method, which calls factorial method.
Display the formatted results.
Repeats the triangle for as many times as dependent on the user.
Enter 0 or a number greater than 30 to quit.@param String[]args- main method receives an array os strings, namely the arguements specified on the command line.
@return void
*/
public static void main(String[] args)
{int lines;
lines = Console.readInt("WELCOME TO PASCAL'S TRIANGLE \nPlease enter the amount of lines (>0 and <=30) to be displayed>");
System.out.println();while ((lines > 0) && (lines <= 30))
{lines = lines - 1; //lines -the number that tells how many lines to display
long element; int n=0; int r=0; int i=0; int MID = 0;
//MID i-midpoint used to center the Pascal Triangle
//element holds the value of coefficient
for(n=0; n<=lines; n++) //n -present line of the triangle
{ MID=36;//Prints spaces before numbers
//i decrements a certain amount of spaces to print the present line
for(i=MID-n; i>n; i--)
{Format.printf("%1s"," ");}
//Prints all the elements on the same line
for(r=0; r<=n; r++) //r-specific element in the nth line
{ element = combination(n,r);
Format.printf("%4d", element);
}
System.out.println();}
System.out.println();
lines = Console.readInt("Enter another number of lines (>0 and <=30) to be displayed\n or enter '0' to exit program");
//Test for validity of "lines"
if ((lines <= 0) || (lines > 30))
{System.out.println();
System.out.println("Thank you for using Pascal's Triangle\n Author: Kerry-Ann White");
System.out.println("Have a good day !");
System.exit(0);}
System.out.println();} //ends while loop
} //ends method
} //ends class