public class Pascal { public static void triangle(int maxRows) { int row, num; for (int i = 0; i <= maxRows; i++) { num = 1; row = i + 1; for (int col = 0; col <= i; col++) { if (col > 0) { num = num * (row - col) / col; } System.out.print(" " + num + " "); } System.out.println(); } } public static void main(String[] args) { Pascal.triangle(5); } }
Output
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
Thanks for reading.
If you like this post please follow us for more updates about technology related updates.
No comments:
Post a Comment