JavaScript Program to Print Pyramid Pattern by using Numbers Last Updated : 16 Jan, 2024 Comments Improve Suggest changes Like Article Like 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 71 2 3 4 5 6 7 8 9ApproachWe 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); }; Output1 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); }; Output1 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); Output1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 Comment More infoAdvertise with us Next Article JavaScript Program to Print Pyramid Pattern by using Numbers V vishalkumar2204 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-DSA JavaScript-Program Similar Reads JavaScript Program to Print Floydâs Pattern Triangle Pyramid Floyd's Triangle is a right-angled triangular array of natural numbers, used primarily in computer science education. It is named after Robert Floyd. Each row of the triangle corresponds to the sequence of consecutive natural numbers. In this article, we will explore different approaches to implemen 2 min read JavaScript Program to Print Number Pattern The idea of pattern-based programs is to understand the concept of nesting for loops and how and where to place the alphabet/numbers/stars to make the desired pattern. These are the following approaches to printing different types of number patterns:Table of Content Using nested loops Using array ma 2 min read JavaScript Program to Print Pyramid Star Pattern In this article, we will demonstrate how to create pyramid star patterns in JavaScript. The Possible Pyramid Star Patterns are:Upper pyramidInverted pyramidLeft inverted pyramidRight inverted pyramidApproachThere will be two different approaches based on alignments i.e. horizontal and vertical. Hori 3 min read JavaScript Program to Print Reverse Floyd Pattern Triangle Pyramid Printing a reverse Floyd pattern triangle pyramid involves outputting a series of numbers in descending order, forming a triangular shape with each row containing one less number than the previous row. The numbers are arranged in such a way that they form a right-angled triangle, reminiscent of Floy 2 min read JavaScript Program to Print Inverted Pyramid In JavaScript, the Inverted Pyramid is the geometric pattern of "*" that is arranged in the format upside-down. This can be printed using various approaches like Looping, Recursion, and built-in methods. Table of Content Using Nested for LoopUsing RecursionUsing Array and Join methodsUsing Nested fo 3 min read JavaScript Program to Print 180 Rotation of Simple Pyramid A program to print 180 rotations of a simple pyramid involves iterating through a loop 180 times, each time printing the pyramid with a different orientation. This is achieved by adjusting the number of spaces and asterisks printed in each row to create the desired pyramid shape. There are several w 2 min read JavaScript Program for Hollow Pyramid Star Pattern In this article, we will demonstrate how to create Hollow Pyramid Star Patterns in JavaScript. Possible Hollow Pyramid Star Patterns areUpper pyramidInverted pyramidLeft inverted pyramidRight inverted pyramidApproachThere will be two different approaches based on alignments i.e. horizontal and verti 3 min read JavaScript Program to Print Double Sided Stair-Case Pattern This JavaScript program is designed to print a double-sided staircase pattern. A double-sided staircase consists of steps where each step has numbers incrementing from 1 to the current row number on the left side, and then decrementing from the current row number - 1 down to 1 on the right side. The 3 min read JavaScript Program to Print Inverted Hollow Star Pyramid In this article, we will write a Program to Print an Inverted Hollow Star pyramid. Possible Inverted Hollow Star Pyramid Patterns are Inverted pyramidLeft inverted pyramidRight inverted pyramidThere will be two different approaches based on alignments i.e. horizontal and vertical:Table of Content Ho 3 min read JavaScript Program to Print Hollow Star Pyramid in a Diamond Shape The JavaScript program is designed to print a hollow star pyramid in a diamond shape. This task involves generating a pattern of stars arranged in a pyramid-like shape with hollow spaces in between, forming a diamond silhouette. The program aims to provide a flexible solution that can accommodate di 3 min read Like