0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Print Floyd Triangle

You can print Floyd's triangle in JavaScript using nested loops like this: let num = 1; for(let i=1; i<=rows; i++){ let row = ''; for(let j=1; j<=i; j++){ row += num++ + ' '; } console.log(row); } where rows is the number of rows you want.
📋

Examples

Inputrows = 1
Output1
Inputrows = 3
Output1 2 3 4 5 6
Inputrows = 5
Output1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
🧠

How to Think About It

To print Floyd's triangle, start with number 1 and print numbers in increasing order row by row. Each row has one more number than the previous. Use one loop for rows and a nested loop for numbers in each row, increasing the number after printing each.
📐

Algorithm

1
Get the number of rows to print.
2
Initialize a number variable to 1.
3
For each row from 1 to the number of rows:
4
Create an empty string for the current row.
5
For each position in the current row:
6
Append the current number and a space to the row string.
7
Increment the number by 1.
8
Print the row string.
💻

Code

javascript
const rows = 5;
let num = 1;
for (let i = 1; i <= rows; i++) {
  let row = '';
  for (let j = 1; j <= i; j++) {
    row += num++ + ' ';
  }
  console.log(row);
}
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
🔍

Dry Run

Let's trace printing Floyd's triangle for 3 rows through the code.

1

Initialize

rows = 3, num = 1

2

Row 1 loop

i=1, row='', inner loop runs once: row='1 ', num=2

3

Print Row 1

Output: '1 '

4

Row 2 loop

i=2, row='', inner loop runs twice: row='2 3 ', num=4

5

Print Row 2

Output: '2 3 '

6

Row 3 loop

i=3, row='', inner loop runs thrice: row='4 5 6 ', num=7

7

Print Row 3

Output: '4 5 6 '

Row (i)Numbers Printednum after row
112
22 34
34 5 67
💡

Why This Works

Step 1: Outer loop controls rows

The outer for loop runs from 1 to the number of rows, deciding how many rows to print.

Step 2: Inner loop prints numbers per row

The inner for loop runs as many times as the current row number, printing that many numbers.

Step 3: Number increments after each print

After printing each number, we increase num by 1 to get the next number in the triangle.

Step 4: Print each row

We build a string for each row and print it, so the triangle forms line by line.

🔄

Alternative Approaches

Using a single loop and math
javascript
const rows = 5;
let num = 1;
for (let i = 1; i <= (rows * (rows + 1)) / 2; i++) {
  process.stdout.write(num++ + ' ');
  if (i === (Math.sqrt(8 * i + 1) - 1) / 2) {
    console.log('');
  }
}
This uses math to detect row ends but is less readable and more complex.
Using recursion
javascript
function printFloyd(rows, current = 1, row = 1) {
  if (row > rows) return;
  let line = '';
  for (let i = 0; i < row; i++) {
    line += current++ + ' ';
  }
  console.log(line);
  printFloyd(rows, current, row + 1);
}
printFloyd(5);
Recursion can print Floyd's triangle but uses more memory and is less straightforward.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses nested loops where the inner loop runs increasing times per row, totaling about n(n+1)/2 operations, which is O(n^2).

Space Complexity

Only a few variables are used for counting and building strings, so space is O(1) constant.

Which Approach is Fastest?

The nested loop approach is simplest and fastest for Floyd's triangle. The math or recursion methods add complexity without speed benefits.

ApproachTimeSpaceBest For
Nested loopsO(n^2)O(1)Readability and simplicity
Single loop with mathO(n^2)O(1)Compact but complex logic
RecursionO(n^2)O(n)Demonstrating recursion, less efficient
💡
Use nested loops where the outer loop controls rows and the inner loop prints numbers per row.
⚠️
Beginners often forget to increment the number after printing, causing repeated numbers.