Saturday, August 22, 2026

Printing Inverted Full Pyramid in C | Advanced Shape Patterns

The inverted full pyramid is a fundamental structural pattern in C programming that tests your command over multi-loop logic, decreasing iteration bounds, and symmetric space management.

Introduction

Mastering the inverted full pyramid sharpens your ability to manage dynamic loop bounds and horizontal symmetry. This tutorial breaks down the pattern logic, line-by-line algorithm, C source code, and performance complexity required to output a flexible inverted pyramid based on user input.

Prerequisites: Familiarity with basic for loops, standard I/O (printf, scanf), and integer arithmetic.

Expected Output:

*********
 *******
  *****
   ***
    *

Deconstructing the Pattern Logic

To render an inverted pyramid of height $n$, track how the leading space count increases while the asterisk count decreases across rows:

Row (i)Leading Spaces (i−1)Asterisks (2(n−i)+1)
109
217
325
433
541
  • Outer Loop: Iterates through rows $i = 1$ to $n$.

  • Spaces Loop: Prints $i - 1$ spaces on row $i$ to shift the asterisks rightward.

  • Asterisk Loop: Prints an odd sequence of asterisks given by $2(n - i) + 1$.

Code Implementation

#include <stdio.h>

int main() {
    int n, i, j, space;

    printf("Enter the number of rows: ");
    if (scanf("%d", &n) != 1 || n <= 0) {
        printf("Invalid input. Please enter a positive integer.\n");
        return 1;
    }

    for (i = 1; i <= n; i++) {
        // Print leading spaces
        for (space = 1; space < i; space++) {
            printf(" ");
        }
        // Print asterisks
        for (j = 1; j <= (2 * (n - i) + 1); j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Code Breakdown

  • Space Offsets: For row i, the loop runs i - 1 times to maintain vertical pyramid alignment.

  • Star Count Formula: 2(n - i) + 1 calculates the decremental odd sequence (9, 7, 5, 3, 1 for n = 5).

  • Row Transition: printf("\n"); breaks the output line after completing star output for the current row.

Compiling and Execution

Compile and execute the program using standard GCC tools:

Console Output:

Enter the number of rows: 5
*********
 *******
  *****
   ***
    *

Common Mistakes & Troubleshooting

  • Incorrect Formula: Using $2n - 2i$ instead of $2(n - i) + 1$ produces even numbers of stars, distorting the central vertex.

  • Missing Line Breaks: Forgetting printf("\n"); causes all symbols to print on a single continuous horizontal line.

Complexity Analysis

  • Time Complexity: O(n^2) due to nested space and star loop execution over n iterations.

  • Space Complexity: O(1) auxiliary space as it executes using scalar integer variables.

Conclusion

Understanding inverse pattern formulas provides a foundation for complex ASCII graphics and symmetric layout algorithms in lower-level programming.



For all Pattern Programs list click here

…till the next post, bye-bye & take care