Introduction
Pascal's Triangle is a triangular arrangement of binomial coefficients. Each entry in Pascal's Triangle is the sum of the two entries directly above it. This pattern is a great exercise for practicing loops and understanding how to generate binomial coefficients in JavaScript.
Problem Statement
Create a JavaScript program that:
- Accepts the number of rows for Pascal's Triangle.
- Prints Pascal's Triangle in a triangular format.
Example:
- Input:
rows = 5
- Output:
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
Solution Steps
- Input the Number of Rows: The user specifies how many rows of Pascal's Triangle to generate.
- Use Nested Loops: The outer loop handles the rows, and the inner loop handles calculating and printing the elements of Pascal's Triangle.
- Generate Pascal's Triangle: Each value is calculated using the binomial coefficient formula
C(n, k) = n! / (k! * (n - k)!)
. - Display Pascal’s Triangle: Print the triangle in a formatted manner.
JavaScript Program
// Step 1: Function to calculate factorial
function factorial(n) {
let fact = 1;
for (let i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
// Step 2: Function to calculate binomial coefficient C(n, k)
function binomialCoefficient(n, k) {
return factorial(n) / (factorial(k) * factorial(n - k));
}
// Step 3: Input the number of rows for Pascal's Triangle
let rows = parseInt(prompt("Enter the number of rows: "));
// Step 4: Outer loop for each row of Pascal's Triangle
for (let i = 0; i < rows; i++) {
let output = '';
// Step 5: Print leading spaces for alignment
for (let j = 0; j < rows - i - 1; j++) {
output += ' ';
}
// Step 6: Inner loop to calculate and print the numbers for each row
for (let k = 0; k <= i; k++) {
output += binomialCoefficient(i, k) + ' ';
}
// Print the output for the current row
console.log(output.trim());
}
Explanation
Step 1: Factorial Function
- The function
factorial(n)
calculates the factorial of a numbern
. Factorial is used to calculate the binomial coefficient for Pascal's Triangle.
Step 2: Binomial Coefficient Function
- The function
binomialCoefficient(n, k)
calculates the binomial coefficientC(n, k)
using the formulaC(n, k) = n! / (k! * (n - k)!)
.
Step 3: Input the Number of Rows
- The program asks the user to input how many rows of Pascal's Triangle to generate.
Step 4: Outer Loop for Rows
- The outer loop controls how many rows are printed. Each iteration represents one row of Pascal's Triangle.
Step 5: Print Leading Spaces for Alignment
- Leading spaces are printed before the numbers to align the triangle correctly.
Step 6: Inner Loop for Calculating and Printing Numbers
- The inner loop calculates and prints each element in the current row using the
binomialCoefficient()
function.
Step 7: Output the Row
- After constructing the row, it is printed using
console.log()
.
Output Example
For rows = 5
, the output will be:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
For rows = 6
, the output will be:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Conclusion
This JavaScript program prints Pascal's Triangle using nested loops. The numbers in each row are calculated using the binomial coefficient formula, and the triangle is formatted with leading spaces for alignment. This exercise is useful for practicing loops, mathematical calculations, and formatting in JavaScript.
Comments
Post a Comment
Leave Comment