Skip to content

Instantly share code, notes, and snippets.

@iporsut
Created June 14, 2011 02:55
Show Gist options
  • Save iporsut/1024215 to your computer and use it in GitHub Desktop.
Save iporsut/1024215 to your computer and use it in GitHub Desktop.
Print Pascal Triangle
public class PascalTriangle {
public static long fact(long n) {
if (n == 0)
return 1;
if (n == 1)
return 1;
long f = 1;
for (long i = 2; i <= n; i++) {
f *= i;
}
return f;
}
public static long comb(long n, long r) {
return fact(n) / (fact(r)*fact(n-r));
}
public static void main(String []args) {
System.out.print("Insert input : ");
int input = Integer.valueOf((System.console()).readLine());
for (int i = 0; i < input; i++) {
for (int s = input-1; s > i; s--)
System.out.print(" ");
for (int j = 0; j <= i; j++) {
System.out.print(comb(i,j) + " ");
}
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment