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

Inverted Alphabet Triangle (A B C D E, A B C D)

The inverted alphabet triangle starts with A B C D E and drops the last letter on every row, ending with a single A. It is the alphabet triangle turned upside down.

Quick answer: Row i prints n βˆ’ i + 1 letters, starting at A each time. The letter at position j is 'A' + j βˆ’ 1, followed by a space. For five rows the first row reads A B C D E and the last row is a lone A.

This program is part of the String Patterns collection.

Table of contents8 sections
  1. Inverted Alphabet Triangle output
  2. How the inverted alphabet triangle works
  3. Inverted Alphabet Triangle in C
  4. Inverted Alphabet Triangle in C++
  5. Inverted Alphabet Triangle in Java
  6. Inverted Alphabet Triangle in Python
  7. Time and space complexity
  8. Frequently asked questions

Inverted Alphabet Triangle output

Output (n = 5):

A B C D E
A B C D
A B C
A B
A
Inverted Alphabet Triangle layout diagram, row by row (n = 5)
How each row of the inverted alphabet triangle is built (n = 5). Blue cells are printed characters; grey dots are spaces.

How the inverted alphabet triangle works

Turning a triangle upside down only changes the row length: n βˆ’ i + 1 instead of i. The letter depends on the position alone, so every row still starts with A and counts up. Because the rows are left-aligned, all the As line up in the first column.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Print n βˆ’ i + 1 letters (letter number j).
  3. Move to the next line.

Key takeaways

  • The row length n βˆ’ i + 1 makes the triangle shrink.
  • The letter comes from the position, so every row starts at A.
  • Each column holds one letter all the way down: A in column 1, B in column 2.
  • Centre the rows with i βˆ’ 1 spaces to get the inverted alphabet pyramid.

Inverted Alphabet Triangle in C

#include <stdio.h>

int main(void) {
    int n = 5;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i + 1; j++)
            printf("%c ", 'A' + j - 1);
        printf("\n");
    }
    return 0;
}

Inverted Alphabet Triangle in C++

#include <iostream>
using namespace std;

int main() {
    int n = 5;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i + 1; j++)
            cout << char('A' + j - 1) << " ";
        cout << endl;
    }
    return 0;
}

Inverted Alphabet Triangle in Java

public class InvertedAlphabetTriangle {
    public static void main(String[] args) {
        int n = 5;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i + 1; j++)
                System.out.print((char) ('A' + j - 1) + " ");
            System.out.println();
        }
    }
}

Inverted Alphabet Triangle in Python

n = 5

for i in range(1, n + 1):
    for j in range(1, n - i + 2):
        print(chr(ord('A') + j - 1), end=" ")
    print()

Time and space complexity

The program prints one character for each position in the pattern, so for n rows it runs in O(nΒ²) time. It only needs a few loop variables, so the extra memory is O(1).

Frequently asked questions

How do I make each row end at E instead?

Start each row later: print 'A' + i + j βˆ’ 2. The rows become A B C D E, B C D E, C D E, D E, E.

How do I print it without spaces between letters?

Drop the space after each letter. The rows become ABCDE, ABCD, ABC, AB, A.

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.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 22 of 22 in String Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

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.

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.