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

Inverted Alphabet Pyramid

The inverted alphabet pyramid starts with A B C D E and loses the last letter on every row, ending with a single A at the bottom. It is the alphabet pyramid upside down.

Quick answer: Row i prints i βˆ’ 1 spaces and then the first n βˆ’ i + 1 letters, each followed by a space. Every row still starts at A; only the length shrinks, so the second row is A B C D and the last row is a single A, four spaces in from the left edge.

This program is part of the Pyramid Patterns collection.

Table of contents8 sections
  1. Output
  2. How the inverted alphabet pyramid works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Inverted Alphabet Pyramid output

Output (n = 5):

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

Same shape, other characters: the Inverted Number Pyramid program draws this same outline with different characters. Putting them side by side shows which part of the loop decides the shape and which part decides what is printed.

How the inverted alphabet pyramid works

Turning a centred pyramid upside down swaps two things: the indent grows instead of shrinking, and the row length shrinks instead of growing. The letter formula, 'A' + j βˆ’ 1, depends only on the position, so it is unchanged. That is why the first column stays A on every row.

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

Key takeaways

  • The indent i βˆ’ 1 grows while the row loses one letter.
  • The letter depends only on the position, so every row starts at A.
  • Each letter plus its space is two characters, which keeps the rows centred.
  • Printing the alphabet pyramid and then this one from row 2 gives an alphabet diamond.

Inverted Alphabet Pyramid in C

#include <stdio.h>

int main(void) {
    int n = 5;

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

Inverted Alphabet Pyramid in C++

#include <iostream>
using namespace std;

int main() {
    int n = 5;

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

Inverted Alphabet Pyramid in Java

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

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

Inverted Alphabet Pyramid in Python

n = 5

for i in range(1, n + 1):
    for j in range(1, i):
        print(" ", end="")
    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?

Print 'A' + i + j βˆ’ 2: row 2 then reads B C D E and the last row is E.

Why does every row start with A?

The letter is worked out from the position j alone, and position 1 is always A. Only the number of positions on the row changes.

How do I join it to the upright pyramid?

Print the alphabet pyramid first, then this pattern starting from row 2 so the widest row is not repeated.

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 16 of 19 in Pyramid 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.