Skip to content
Tutorials Center
My learning

Topics

Lesson 164 of 171 ยท Practice and Review

C++ Programs: Pattern Printing

C++ pattern printing programs with output: half and inverted pyramids, hollow shapes, diamond, hollow diamond, Floyd's triangle and number and letter patterns.

Pattern printing is a classic warm-up because it trains one skill: turning a picture into loops. Every pattern is a grid of rows and columns, and each row is described by a few counts: how many spaces come first, how many symbols follow, and which symbol goes where. Once you can write those counts as functions of the row number, the code nearly writes itself.

This page collects ten patterns, from a plain half pyramid to a hollow diamond and Floyd's triangle. Every program is complete: press Run, change the size n, and watch the shape grow. They use std::string(count, ch) to build a run of identical characters, so most rows need no inner loop at all. If you want to practise loops first, see for loops, and for more classic programs see 40 classic C++ programs.

When a pattern looks hard, number the rows from 1 and write down, for row i, the number of leading spaces and the number of symbols. For a centered pyramid of height n that is n - i spaces and 2 * i - 1 stars. Everything else is a variation.

Pyramids

1. Half pyramid and inverted half pyramid

Row i prints i stars; the inverted version prints n - i + 1. A std::string constructor repeats the character for us.

C++ 1. Half pyramids

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

2. Right-aligned half pyramid

Add n - i leading spaces to push the stars to the right edge.

C++ 2. Right-aligned pyramid

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

3. Full (centered) pyramid

Row i has n - i spaces and 2 * i - 1 stars. The odd counts 1, 3, 5, ... make the shape symmetric. The inverted pyramid is the same loop run backwards.

C++ 3. Centered pyramid

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

4. Hollow pyramid

Only the first column of stars, the last column, and the final row are drawn. For a middle row, print one star, then 2 * i - 3 spaces, then one star. Row 1 is a single star and the last row is solid.

C++ 4. Hollow pyramid

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

5. Hollow square and hollow rectangle

A cell is a star when it lies on the first or last row or the first or last column. This is the pattern where the condition, not the counting, does the work.

C++ 5. Hollow rectangle

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Diamonds

6. Diamond

A diamond is a pyramid on top of an inverted pyramid. Build the top half with rows 1 to n, then the bottom half with rows n - 1 down to 1, so the widest row is not printed twice.

C++ 6. Diamond

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

7. Hollow diamond

Same skeleton as the diamond, but each row prints only its two edge stars. The helper function handles the one-star tips at the top and bottom.

C++ 7. Hollow diamond

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Number and letter triangles

8. Number triangles

Replace the star with the value of the column or the row. Two classic variants: columns count up (1, 12, 123) and the row number repeats (1, 22, 333). The next program continues numbers across rows.

C++ 8. Number triangles

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

9. Floyd's triangle and the reversed Floyd triangle

Floyd's triangle fills rows with the natural numbers in order: row i holds i numbers, so the counter simply keeps increasing across rows. The reversed version starts from the largest value, which is n * (n + 1) / 2, and counts down.

C++ 9. Floyd's triangle

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

10. Letter patterns and the number pyramid

Characters are just small integers, so 'A' + j walks through the alphabet. The second pattern prints a palindromic number pyramid (1, 121, 12321): count up to i, then back down.

C++ 10. Letters and number pyramid

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Habits worth keeping

  • Decide the row formula first (spaces, then symbols), then write the loop. Rows that need no inner loop are simpler and faster to read.
  • Build a helper function (row, hollowRow) as soon as you print the same shape twice. The diamond programs show why.
  • Use static_cast<char>('A' + j) when you want a letter; adding an int to a char produces an int, and printing that would show a number.
  • Use std::setw from <iomanip> when numbers have different widths. See string formatting.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In a centered pyramid of height n, how many stars does row i (counting from 1) have?

    The star counts are 1, 3, 5, ... so row i has 2 * i - 1 stars, preceded by n - i spaces.
  2. 2. Why does the diamond program's second loop start at n - 1?

    The first loop ends on the widest row. If the second loop started at n, that row would appear twice.
  3. 3. What does std::cout << ('A' + 2) print?

    'A' + 2 is an int (67), so the stream prints the number. Cast to char to print the letter C.
  4. 4. For a hollow rectangle, which cells are drawn as stars?

    Border cells satisfy r == 0 || r == rows - 1 || c == 0 || c == cols - 1.

Summary

  • A pattern is a grid; describe each row by its leading spaces and its symbol count.
  • std::string(count, ch) builds a run of repeated characters without an inner loop.
  • Hollow shapes print only the border: first and last columns plus the first or last row.
  • A diamond is a pyramid plus an inverted pyramid that skips the widest row.
  • Floyd's triangle keeps one counter across all rows; a reversed version counts down from n * (n + 1) / 2.
  • Letters are integers: cast 'A' + j back to char before printing.

Next: C++ Programs: Matrix Problems.