---
title: "Pyramid pattern output program"
subject: "Object Oriented Programming"
chapter: 8
key: "semester_3/object_oriented_programming:pyramid-program"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/pyramid-program
site: BCT Engineering
---

# Pyramid pattern output program

Object Oriented Programming · Chapter 8: Stream Computation for Console and File Input/Output

- Asked in: 2068 Baisakh, 6 marks
- Video: [[MUST WATCH]C++ Pyramid/Pattern full explanation with code](https://www.youtube.com/watch?v=6v5sSK7equE) — hdtutorials
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/pyramid-program
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-8

## Question

Write a program to display the output in pyramid form as follows:
```
   A
  AB
 ABC
ABCD
```

## Short answer

Use nested loops: for i=1 to 4, print (4-i) spaces and then characters 'A' through 'A'+i-1. This produces a right-aligned triangle: three spaces then A, two spaces then AB, one space then ABC, and ABCD.

## Answer

Each line *i* (1 to 4) needs (4 − *i*) leading spaces followed by the first *i* letters starting from 'A'.

```cpp
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int rows = 4;
    for (int i = 1; i <= rows; i++) {
        for (int s = 1; s <= rows - i; s++)   // leading spaces
            cout << ' ';
        for (int j = 0; j < i; j++)           // letters A, B, C ...
            cout << char('A' + j);
        cout << endl;
    }
    return 0;
}
```

**Output**

```text
   A
  AB
 ABC
ABCD
```

Alternative using manipulators: build the string `str` of letters for each line and print `cout << setw(rows) << str << endl;`, which right-aligns it in a field of width 4.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. For row i out of 4 rows, how many leading spaces are printed?
   A. i
   B. 4 - i
   C. i - 1
   D. 4 + i
   Answer: B. Row 1 needs 3 spaces, row 4 needs none, so the count is rows - i. (Key point: Leading spaces)

2. How are the letters of row i printed?
   A. char('A' + j) for j = 0 to i-1
   B. char('A' + i) once
   C. 'A' printed i times
   D. The letters from 'A' to 'D' every row
   Answer: A. The first i letters starting from 'A' are printed using char('A' + j). (Key point: Letters per row)

3. How can setw produce the same pyramid?
   A. cout << setw(i) << rows
   B. cout << setw(rows) << str, right-aligning the letters in width 4
   C. cout << setfill(' ') only
   D. cout << left << str
   Answer: B. Printing the row string in a field of width 4 right-aligns it so the leading spaces appear automatically. (Key point: Manipulator alternative)

## Related in Object Oriented Programming

- [Display N characters using default arguments](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3An-chars-default-args-program) — Object Oriented Programming, chapter 3
- [Write and display 10 employees using file](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aemployee-file-program) — Object Oriented Programming, chapter 8
- [Protected specifier; student/test/result program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprotected-three-class-program) — Object Oriented Programming, chapter 6

## Related in other subjects

- [Pattern: PULCHOWK using unformatted output](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apattern-pulchowk) — Computer Programming, chapter 4
- [SOP/POS forms and canonical form of A+BC+ABC](https://www.bctengineer.com/question.md?key=semester_3%2Fdigital_logic%3Asop-pos-canonical-form) — Digital Logic, chapter 3
- [Suspension bridge triangle ABC; span AB](https://www.bctengineer.com/question.md?key=semester_3%2Fsurveying_i%3Asuspension-bridge-triangle-abc-span-ab) — Surveying I, chapter 7
- [Output of arithmetic expression program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aoutput-expression-xyz) — Computer Programming, chapter 3
