MeetCode - Programming Platform | MeetCode - Programming Solutions Platform

C++ Program to Print Inverted Pyramid with Explanation

C++ Medium Pattern Printing 29 views
This problem helps you practice core C++ fundamentals in a practical way. It builds intuition around pyramid, upside-down, inverted. Let’s break it down step by step so you can implement it confidently.
Back to Questions

Problem Statement

Print upside-down pyramid.
Real Life: Reverse of regular pyramid.

Input Format

An integer n (number of rows).

Output Format

Upside-down pyramid made of *.

Constraints

• n ≥ 1

• Use loops only

Concept Explanation

An upside-down pyramid is the reverse of a regular pyramid.
Stars decrease each row, spaces increase to keep it centered.

Step-by-Step Logic

1.Take input n.

2.Loop row i from n down to 1.

3.Print (n - i) spaces.

4.Print (2*i - 1) stars.

5.Move to next line.

Code Solution

This explanation is written for learning purposes and to help beginners understand the concept clearly.
void pattern_q7_inverted_pyramid() { int n = 5; for(int i = n; i >= 1; i--) { // Print spaces for(int j = 1; j <= n - i; j++) { cout << " "; } // Print stars for(int k = 1; k <= 2*i - 1; k++) { cout << "*"; } cout << endl; } }

Output Example

Input:
4
Output:
******* ***** *** *

Common Mistakes

- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).

Notes & Extra Practice

Solutions (0)

No solutions submitted yet. Be the first!

Prev Next