Open In App

JavaScript Program to Print Pyramid Pattern by using Numbers

A pyramid pattern using numbers is a sequence of numbers arranged in a triangular shape, with each row displaying incrementing or decrementing numbers, creating a visually structured pattern.

Example:



         1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9

Approach

Example 1: We are using the above-explained approach.




let n = 5;
for (let i = 1; i <= n; i++) {
    let str = '';
    let count = 1;
    for (let j = 1; j <= 2 * n; ++j) {
        if (i + j >= n + 1 && (i >= j - n + 1)) {
            // Add a space after each number
            str += count.toString() + ' ';
            count++;
        } else {
            // Add two spaces for the gap
            str += '  ';
        }
    }
 
    console.log(str);
}

Output

        1           
      1 2 3         
    1 2 3 4 5       
  1 2 3 4 5 6 7     
1 2 3 4 5 6 7 8 9   

Example 2: We are making Pyramid pattern by using number in left direction.




let n = 5;
for (let i = 1; i <= n; i++) {
    let str = '';
    let count = 1;
    for (let j = 1; j <= n; ++j) {
        if (i + j >= 1 + n && j <= n) {
            // Add a space after each number
            str += count.toString() + ' ';
            count += 1;
        } else {
            // Add two spaces for the gap
            str += '  ';
        }
    }
 
    console.log(str);
};
for (let i = n - 1; i >= 1; i--) {
    let str = '';
    let count = 1;
    for (let j = 1; j <= n; ++j) {
        if (i + j >= 1 + n && j <= n) {
            // Add a space after each number
            str += count.toString() + ' ';
            count += 1;
        } else {
            // Add two spaces for the gap
            str += '  ';
        }
    }
    console.log(str);
};

Output
        1 
      1 2 
    1 2 3 
  1 2 3 4 
1 2 3 4 5 
  1 2 3 4 
    1 2 3 
      1 2 
        1 

Example 3: We are making Pyramid pattern by using numbers in right direction.




let n = 5;
for (let i = 1; i <= n; i++) {
    let str = "";
    let count = 1; // Add numbers
    for (let k = 1; k <= i; k++) {
        str = count.toString() + " " + str;
        count += 1;
    }
    // Add spaces after numbers
    for (let j = i; j < n; j++) {
        str += "  ";
    }
    console.log(str);
}
for (let i = n - 1; i >= 1; i--) {
    let str = "";
    let count = 1;
    // Add numbers
    for (let k = 1; k <= i; k++) {
        str = count.toString() + " " + str;
        count += 1;
    }
    // Add spaces after numbers
    for (let j = i; j < n; j++) {
        str += "  ";
    }
    console.log(str);
};

Output
1         
2 1       
3 2 1     
4 3 2 1   
5 4 3 2 1 
4 3 2 1   
3 2 1     
2 1       
1         

Example 4: We are generated number pyramid in down-ward direction.




let n = 5;
for (let i = n; i >= 1; i--) {
    let str = '';
    let count = 1;
    for (let j = 1; j <= 2 * n; ++j) {
        if (i + j >= n + 1 && (i >= j - n + 1)) {
            // Add a space after each number
            str += count.toString() + ' ';
            count++;
        } else {
            // Add two spaces for the gap
            str += '  ';
        }
    }
 
    console.log(str);
};

Output
1 2 3 4 5 6 7 8 9   
  1 2 3 4 5 6 7     
    1 2 3 4 5       
      1 2 3         
        1           

Example 5: We are generating Inverted Half Pyramid




function printInvertedHalfPyramid(rows) {
    for (let i = rows; i >= 1; i--) {
        let row = '';
        for (let j = 1; j <= i; j++) {
            row += j + ' ';
        }
        console.log(row);
    }
}
 
// Example usage for an inverted half pyramid with 5 rows
printInvertedHalfPyramid(5);

Output
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 

Article Tags :