Skip to content
Tutorials Center
My learning

Topics

Lesson 130 of 135 ยท Practice and Review

C Pattern Printing Programs

Pattern printing programs in C with output: hollow and inverted pyramids, diamonds, number and letter triangles and reverse Floyd's triangle, with the loop logic explained.

Pattern programs look like a children's game, but they teach the most important loop skill there is: working out how the row number decides how many spaces, stars and digits to print. Once you can write "row r prints n - r spaces, then 2r - 1 stars" without guessing, nested loops stop feeling mysterious.

C Programs: 50 Classic Examples already has a solid pyramid, Floyd's triangle and Pascal's triangle. This page goes further: hollow and inverted shapes, diamonds, and triangles of numbers and letters. Every program compiles as it is. Change rows and watch the shape grow.

How to design any pattern

Do the same three steps every time:

  1. Count the rows. That is the outer loop.
  2. For each row, write down how many of each thing is printed (spaces, stars, digits) as a formula in the row number.
  3. Each formula becomes one inner loop, or one printf("%*s", width, "") call, which prints width spaces without a loop.

Print '\n' once at the end of every row, and nothing else touches the line break.

Inverted and hollow pyramids

An inverted pyramid is the pyramid run backwards: row r (counting from 1) prints r - 1 spaces and 2 * (rows - r) + 1 stars. A hollow pyramid prints stars only on the two edges and on the last row. The test inside the inner loop is the whole trick.

C Inverted and hollow 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 scanf or fgets), it gets the next line from this box.

In the hollow version a cell is a star when it is the first cell of the row (col == 0), the last cell (col == width - 1) or anywhere on the bottom row (row == rows). Row 1 has width 1, so its single cell is both first and last.

Diamonds

A diamond is a pyramid on top of an inverted pyramid that shares no row twice. The widest row is printed once. Loop row from 1 up to rows, then from rows - 1 back down to 1, and reuse one function that prints a row of a given width.

C Solid and 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 scanf or fgets), it gets the next line from this box.

bool is a keyword in C23, so no header is needed. The helper keeps both the top half and the bottom half identical in logic, which is why the shape stays symmetric. Passing the same function a different hollow flag gives the two diamonds.

Number and letter triangles

Digits and letters are just integers and char values. Print col for a number triangle, or 'A' + col for letters, because the letters of the alphabet have consecutive codes in ASCII and UTF-8.

C Number and letter 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 scanf or fgets), it gets the next line from this box.

The last triangle keeps a running letter across rows, wrapping back to 'A' after 'Z'. Whenever a pattern continues from row to row, as Floyd's triangle does with numbers, the counter lives outside the loops.

Reverse Floyd's triangle

Floyd's triangle counts up from 1. The reverse version starts from the last number and counts down, with the longest row first. The total count is rows * (rows + 1) / 2, so start there and decrement.

C Reverse 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 scanf or fgets), it gets the next line from this box.

Row 5 prints 15 down to 11, row 4 prints 10 down to 7, and so on until a single 1. Try changing the format to %-3d to left-align, or swap to number++ and a reversed start to build other variants.

Tip: When a pattern comes out lopsided, print the row number and each count in a quick debug line first. The bug is nearly always an off-by-one in one of the formulas, and seeing the counts makes it obvious.

Hourglass

An hourglass is an inverted pyramid on top of a normal one, sharing the single-star middle row. With n rows in each half there are 2 * n - 1 rows. For row number row (from 1), the star count is 2 * (n - row) + 1 in the top half and 2 * (row - n) + 1 in the bottom half, and the leading spaces are whatever is left, divided by two. The second loop prints a hollow version of the top half, where only the first and last cell of each row are stars.

C Hourglass pattern

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 scanf or fgets), it gets the next line from this box.

The trick is to turn "two halves" into one formula with a conditional, so a single loop prints the whole shape.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does printf("%*s", 4, "") print?

    The * takes the field width from the argument list, so the empty string is padded with spaces to a width of 4.
  2. 2. In the hollow pyramid, why is the test row == rows part of the star condition?

    Middle cells of a row are blank, except on the last row, which closes the shape with a solid base.
  3. 3. How many stars does row r (counted from 1) of a pyramid print?

    The count is 1, 3, 5, 7, ..., so each row adds two stars to the row before it.
  4. 4. Why does putchar('A' + col) print letters?

    A char is a small integer. The alphabet's codes are consecutive in ASCII, so adding an offset moves along the alphabet.

Summary

  • Pick the row count, then express every printed piece as a formula in the row number.
  • printf("%*s", n, "") prints n spaces; putchar('\n') ends each row.
  • Hollow shapes test for the edge cells (first, last, bottom row) inside the inner loop.
  • A diamond is a pyramid plus an inverted pyramid that skips the widest row the second time; one helper function keeps both halves consistent.
  • Letters are integers: 'A' + col walks the alphabet. State that carries across rows lives outside the loops.

Next: C Practice Exercises, where you write programs like these yourself.