On my last job interview, i was asked to create a very simple Pascal Triangle using java. What is a Pascal Triangle (indonesian people called it “Segitiga Pascal”) ?? According to Wikipedia, it is a triangular array of the binomial coefficients in a triangle. Okay this is how i do it.

```package com.edw.test;

/**
*  com.edw.test.PascalTriangle
*
*  @author edw
*/
public class PascalTriangle {

public static void main(String[] args) {
// initiate
int numberOfRows = 8;
int[][] pascal = new int[numberOfRows][numberOfRows];

// fill my triangle
for (int i = 0; i &lt; numberOfRows; i++) {
pascal[i][0] = 1;
for (int j = 1; j &lt;= i; j++) {
pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j];
}
}

// print it
for (int i = 0; i &lt; numberOfRows; i++) {
for (int j = 0; j &lt;= i; j++) {
System.out.print(pascal[i][j] + &quot; &quot;);
}
System.out.println();
}
}
}
```

this is the result on my Netbeans’ console

hope it can help others
cheers

Exile829

about 5 years ago

Thank you so much! (Y)

edwin

about 5 years ago

You're welcome