C++ Program to Print Inverted Pyramid with Explanation
C++
Medium
Pattern Printing
29 views
1 min read
134 words
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.
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
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).
Solution Guide
Problem
Print upside-down pyramid.
Real Life: Reverse of regular pyramid.
Input / Output
Input
An integer n (number of rows).
Output
Upside-down pyramid made of *.
Constraints
• n ≥ 1
• Use loops only
Examples
Output:
*******
*****
***
*
Explanation
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 Explanation
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.
Details
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).
Official Solution
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;
}
}
Solutions (0)
No solutions submitted yet. Be the first!