Open In App

JavaScript Program to Print Pyramid Pattern by using Numbers

Last Updated : 16 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • We initialize a variable n with the value 5. This variable determines the number of rows in the pattern.
  • We use a for loop with the variable i to iterate from 1 to n (inclusive). This loop controls the rows of the pattern.
  • Inside the outer loop, there’s another for loop with the variable j, iterating from 1 to 2 * n (twice the number of columns). This loop controls the columns of the pattern.
  • we use Conditional logic to checks if (i + j >= n + 1 && i >= j – n + 1) for number or gap. Numbers increment; otherwise, two spaces are added.
  • After the inner loop, print str (one row). Repeat for each i, forming the desired pattern.

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

Javascript




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.

Javascript




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.

Javascript




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.

Javascript




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

Javascript




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 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads