Pascal's Triangle
/**
This Java application generates Pascal's Triangle. The number
of lines to be displayed is dependent upon user input. After generating the triangle, the application then prompts the user to enter a "1" to generate another triangle or enter any other key to exit.@version 1.01 25 Sep 1999
@author Zarinah El-Amin
@Project 1
*/
import corejava.*; // imports corejava package
public class el_amin_1
{
/**
This method computes the factorial value for a value
@param n is the number to compute n!
@returns the factorial result
*/
public static long factorial(int n)
{
long result = 1;
for (int i = 1; i <= n; i++)
{ result *= i;}
return result;
}
/**
This is the main method.
@returns none
@throws An illegal value entered Message if bad value
*/
public static void main(String[] args)
{
int generate = 1; //If generate is 1 then continue
//generating triangles, if not exit
System.out.print("Welcome to Zarinah's Pascal's Triangle Generator! Thanks for coming and enjoy your stay!");
System.out.println();
//Main loop to generate the Pascal triangles. It uses the formula
//for combinations n!/r!(n-r)! in order to compute the coefficients
while(generate == 1)
{
int y=35; int x =35; //Controls the spacing of
//the triangle
System.out.println();
//Prompt the user for the number of Pascal Triangle to generate
int lines =Console.readInt
("Please enter the number of lines you would like in your Pascal's Triangle up to 13:");
//If user enters an incorrect value prompt for a new value
while ((lines > 13) || (lines < 1))
{
lines = Console.readInt("You entered an incorrect number please enter a number greater than 0 and less than 13. Thank You.");
}
long nf; //Contains lines! value
long term; //Contains the coefficent
System.out.println();
System.out.print("Thank You for using Zarinah's Pascal's Triangle generator");
System.out.println();
System.out.print("This is your completed Triangle.");
System.out.println();
System.out.print("*******************************************************************************");
System.out.println();
//Loop to generate lines # of Pascal triangle
for (int n = 0; n < lines; n++)
{
//Invoke factorial method to
//compute the numerator of the
//Combination formula
nf = factorial(n);
//Format the spacing of the triangle
while (y >0)
{
System.out.print(" ");
y--;
}
//Compute and output the coefficients
for (int r = 0; r <= n; r++)
{
term = nf / (factorial(r) * factorial(n-r));
Format.printf("%4d", term);
}
System.out.println();
x -= 2; //Decrement x in order to align the
//rows of the triangle
y = x; //Reset y for the next line
}
System.out.println();
System.out.print("*******************************************************************************");
System.out.println();
System.out.print("If you would like to generate another triangle please enter 1.");
System.out.println();
//If the user enters 1 then a new triangle may be generated.
//If any other number is entered then the program is exited
generate = Console.readInt("To exit simply hit any other number.");
//Causes program to exit
if (generate !=1)
break;
}
}
}