Java Program to Generate Pascal's Triangle

Pascal’s Triangle is a pattern of numbers where each number is the sum of the two numbers directly above it. It's called a triangle because the numbers are arranged in rows, and the first row is just a single number, "1." Each subsequent row builds on the previous one.

Here’s how the first few rows of Pascal's Triangle look.

Row 0: 1 Row 1: 1 1 Row 2: 1 2 1 Row 3: 1 3 3 1 Row 4: 1 4 6 4 1

  • The first and last numbers in each row are always 1.
  • The numbers in the middle are the sum of the two numbers directly above the row before it.

Java Program to Generate Pascal's Triangle

Here's a simple Java program that generates Pascal's Triangle for a given number of rows.

import java.util.Scanner;

public class PascalsTriangle {

    public static void main(String[] args) {
        // Create a Scanner to read user input
        Scanner scanner = new Scanner(System.in);

        // Ask the user to enter the number of rows
        System.out.print("Enter the number of rows for Pascal's Triangle: ");
        int rows = scanner.nextInt();

        // Loop to generate each row of the triangle
        for (int i = 0; i < rows; i++) {
            // Print spaces to align the numbers in a triangle shape
            for (int j = 0; j < rows - i - 1; j++) {
                System.out.print(" ");
            }

            // Print the numbers in each row
            int value = 1; // The first number in each row is always 1
            for (int j = 0; j <= i; j++) {
                System.out.print(value + " ");

                // Calculate the next number in the row
                value = value * (i - j) / (j + 1);
            }

            // Move to the next line after printing a row
            System.out.println();
        }

        // Close the scanner
        scanner.close();
    }
}

How does the Program Work?

  1. User Input: The program starts by asking the user to input how many rows of Pascal's Triangle they want to generate.
  2. Outer Loop: The outer loop is responsible for creating each row of the triangle. It runs once for every row the user wants to display.
  3. Printing Spaces: Before printing the numbers for each row, the program adds spaces to center-align the numbers, ensuring that the triangle looks symmetrical.
  4. Inner Loop: The inner loop prints the numbers for the current row. It starts with 1, and then calculates the next number in the row using a mathematical formula that adds the numbers from the row above.
  5. Line Break: After each row of numbers is printed, the program moves to the next line to print the next row of numbers.

Example Output

If you enter 5 for the number of rows, the output will look like this.

Conclusion

This Java program generates Pascal’s Triangle, which is useful in many areas of mathematics. The program uses loops and simple calculations to create each row and align them properly. Pascal’s Triangle has many applications, especially in combinatorics, and this program helps you learn how to work with loops and calculations in Java.