public class PascalTriangleDubs {
//Where the triangle is created and displayed
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of rows you want displayed from Pascal's Triangle: ");
int numRows = in.nextInt(); //stores number of rows user wants displayed
displayTriangle(pascalTriangle(numRows));
} //end main
public static int[][] pascalTriangle(int n){
int[][] triangle = new int[n][]; //creates a matrix to store the triangle
for(int i = 0; i < n; i++){
triangle[i] = new int[i+1];
for(int j = 0; j < triangle[i].length; j++){
triangle[i][j] = choose(i, j);
}
}
return(triangle);
}
public static int choose(int n, int k){
return(factorial(n))/(factorial(k)*factorial(n-k));
}
public static int factorial(int num){
int var = 1;
if(num == 0)
return(1);
for(int i = 1; i var *= i;
return(var);
}
public static void displayTriangle(int[][] triangle){
for(int i = 0; i < triangle.length; i++){
for(int j = 0; j < triangle[i].length; j++){
System.out.print(triangle[i][j] + "\t");
}
System.out.println();
}
}
}
Published by Chris Chen
Chris is currently attending the University of California, Berkeley seeking an undergraduate's degree in Electrical Engineering Computer Science. He enjoys playing basketball, practicing kendo, hanging out w... View profile
- Blaise Pascal's Contributions to MathTells about the life and contributions of Blaise Pascal to math.
A Timeline of Roulette HistoryRoulette is one of the most popular gambling games in the world, particularly in Europe where it originated and the United States. It was invented by the famed mathematician Bl...- MBTA the Boston Subway System: How it WorksThis is an overview of the Boston MBTA Subway system at large. Breaks down the various train lines and gives basic information to help travelers understand the MBTA subway system maps more easily.
Review of Garmin Street Pilot Portable Car GPS Navigation SystemThe portable GPS navigation systems offered by companies such as Garmin have revolutionized business and personal travel. Read this review to learn the pro's and con's of the G...
EasyEnvelopes - Print Envelopes Right from Your Mac's DashboardEasyEnvelopes is a free utility that makes it painless and easy to print out envelopes right from inside your Dashboard. It can access your Address Book, so you hardly need to...
- Your First Java Program
- What is Pascal's Triangle?
- Best Practices to Be Followed in JAVA
- Be the Gangster John Dillinger for Halloween; See How to Match Johnny Depp's Costu...
- History of Blaise Pascal
- "Access to Path 'xx' is Denied." - IIS "Double Hop" Issue
- A Guide to Start Programming in Java



