JavaScript Program to Print Reverse Floyd Pattern Triangle Pyramid
Last Updated :
16 Feb, 2024
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
Share your thoughts in the comments
Please Login to comment...