New: 201 pattern programs with tested code in C, C++, Java and Python Explore patterns

Reverse Floyd’s Triangle

Read summary with AI tools

Reverse Floyd’s triangle is Floyd’s triangle turned upside down. The longest row comes first and the numbers count down, so five rows start at 15 and end at 1.

Quick answer: Work out the largest number first: n × (n + 1) / 2, which is 15 for five rows. Then run the outer loop from i = n down to 1 and print i numbers on each row, decreasing the value by one after every print. The counter keeps going down across rows, so the last row prints 1.

This program is part of the Series Patterns collection.

Table of contents8 sections
  1. Reverse Floyd’s Triangle output
  2. How the reverse floyd’s triangle works
  3. Reverse Floyd’s Triangle in C
  4. Reverse Floyd’s Triangle in C++
  5. Reverse Floyd’s Triangle in Java
  6. Reverse Floyd’s Triangle in Python
  7. Time and space complexity
  8. Frequently asked questions

Reverse Floyd’s Triangle output

Output (n = 5):

 15 14 13 12 11
 10  9  8  7
  6  5  4
  3  2
  1
Reverse Floyd's Triangle layout diagram, row by row (n = 5)
The numbers of the reverse floyd’s triangle laid out row by row (n = 5).

How the reverse floyd’s triangle works

Floyd’s triangle has 1 + 2 + … + n numbers in total, which is n(n + 1) / 2. The reverse version prints the same numbers in the opposite order, so it starts at that total. The row lengths also run backwards: n numbers on the first row and one on the last. A single counter that only ever goes down handles the values; the row loop only decides when to break the line.

  1. Set value = n × (n + 1) / 2.
  2. Run the outer loop for i from n down to 1.
  3. Print i numbers, each three characters wide, taking one off value after each print.
  4. Move to the next line.

Key takeaways

  • The first number is the triangle number n(n + 1) / 2.
  • The outer loop counts down, so the rows get shorter: 5, 4, 3, 2, 1 numbers.
  • One counter, decreased after every print, produces all the values.
  • value-- prints the current value and then lowers it, in a single step.

Reverse Floyd’s Triangle in C

#include <stdio.h>

int main(void) {
    int n = 5;
    int value = n * (n + 1) / 2;

    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= i; j++)
            printf("%3d", value--);
        printf("\n");
    }
    return 0;
}

Reverse Floyd’s Triangle in C++

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int n = 5;
    int value = n * (n + 1) / 2;

    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= i; j++)
            cout << setw(3) << value--;
        cout << endl;
    }
    return 0;
}

Reverse Floyd’s Triangle in Java

public class ReverseFloydsTriangle {
    public static void main(String[] args) {
        int n = 5;
        int value = n * (n + 1) / 2;

        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= i; j++)
                System.out.printf("%3d", value--);
            System.out.println();
        }
    }
}

Reverse Floyd’s Triangle in Python

n = 5
value = n * (n + 1) // 2

for i in range(n, 0, -1):
    for j in range(i):
        print(f"{value:3d}", end="")
        value -= 1
    print()

Time and space complexity

The program prints n(n + 1) / 2 numbers with constant work for each, so it runs in O(n²) time and uses O(1) extra memory.

Frequently asked questions

What is the difference between Floyd’s triangle and the reverse version?

Both use the numbers 1 to n(n + 1) / 2. Floyd’s triangle counts up and grows by one number per row. The reverse version counts down and shrinks by one number per row, so it looks like Floyd’s triangle read from the bottom right.

Can I keep the short rows at the top and still count down?

Yes. Keep value = n × (n + 1) / 2 but run the outer loop from 1 to n. The output becomes 15, then 14 13, then 12 11 10, and so on, with the longest row at the bottom.

Why print each number three characters wide?

Two-digit and one-digit numbers would otherwise push the columns out of line. A width of 3 fits numbers up to 99, which covers 13 rows (91 numbers). For more rows, raise the width to 4.

How do I let the user choose the number of rows?

Read n from the keyboard instead of fixing it in the code: scanf("%d", &n); in C, cin >> n; in C++, new Scanner(System.in).nextInt() in Java and int(input()) in Python.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Latest articles

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.