Pascal Pyramid with Spacing
This program prints Pascal's Triangle in pyramid form with spacing.
Problem Statement
Write a Java program that prints Pascal's Triangle with 6 rows, aligned as a pyramid.
Source Code
| 1 | public class PascalPyramid { |
| 2 | public static void main(String[] args) { |
| 3 | int rows = 6; |
| 4 | for(int i=0; i<rows; i++) { |
| 5 | for(int j=i; j<rows; j++) System.out.print(" "); |
| 6 | int num = 1; |
| 7 | for(int j=0; j<=i; j++) { |
| 8 | System.out.print(num + " "); |
| 9 | num = num * (i - j) / (j + 1); |
| 10 | } |
| 11 | System.out.println(); |
| 12 | } |
| 13 | } |
| 14 | } |
Program Output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Explanation
Leading spaces align coefficients centrally while the binomial formula computes each Pascal value, so the numbers increase then decrease symmetrically per row, forming Pascal's Triangle in a centered pyramid alignment.