Horje
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 Floyd’s triangle, but in reverse order.

Example:

Input: 5
Output:
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1

These are the following approaches:

Approach 1: Using nested Loop

Using nested loops, iterate from the specified number of rows downwards, printing numbers in descending order for each row, forming a reverse Floyd pattern triangle pyramid.

Example: The function myFunction generates a reverse Floyd pattern triangle pyramid by iterating through rows downwards, printing descending numbers, and forming a triangle with decreasing lengths per row.

Javascript

function myFunction(rows) {
    let num = rows * (rows + 1) / 2;
    for (let i = rows; i >= 1; i--) {
        let pattern = '';
        for (let j = 1; j <= i; j++) {
            pattern += num-- + ' ';
        }
        console.log(pattern);
    }
}
 
myFunction(5);

Output

15 14 13 12 11 
10 9 8 7 
6 5 4 
3 2 
1 

Approach 2: Using Recursion

The recursion approach recursively prints a reverse Floyd pattern triangle pyramid. It decreases the number of rows in each call, printing descending numbers per row until reaching the base case of zero rows.

Example: The function myFunction recursively generates a reverse Floyd pattern triangle pyramid. It decreases rows in each call, printing descending numbers per row until reaching zero rows.

Javascript

function myFunction(rows, num = rows
    * (rows + 1) / 2) {
    if (rows === 0) return;
    let pattern = '';
    for (let i = 1; i <= rows; i++) {
        pattern += num-- + ' ';
    }
    console.log(pattern);
    myFunction(rows - 1, num);
}
 
myFunction(6);

Output

21 20 19 18 17 16 
15 14 13 12 11 
10 9 8 7 
6 5 4 
3 2 
1 



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
JavaScript Program to Find the Sum of Natural Numbers using Recursion JavaScript Program to Find the Sum of Natural Numbers using Recursion
JavaScript Program to Print Number Pattern JavaScript Program to Print Number Pattern
Cube Sum of First n Natural Numbers in JavaScript Cube Sum of First n Natural Numbers in JavaScript
Execution Context in JavaScript Execution Context in JavaScript
What is BOM( Browser Object Model) ? What is BOM( Browser Object Model) ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14