30 Pattern Programs in C: Stars, Numbers and Alphabets
Pattern programs in C use loops to print rows of stars, numbers or letters. The outer loop chooses the row; inner loops print leading spaces and visible characters; one newline ends the row. This collection contains 30 distinct examples: 16 star shapes, 8 number patterns and 6 alphabet patterns. Each has its own complete C17 program, a loop explanation and reference output for input 5.

How to run these examples
Choose a pattern below. Its output stays visible; expand Show C code to read the complete program, including input validation. The run link opens that exact program in the online C compiler with sample input 5 already supplied. It does not execute code automatically. Press Run in the compiler to execute it, then change the input and predict the next result.
All programs read the first input line: one digit from 1 to 9, optionally surrounded by whitespace, with at most 63 characters before the newline. A final newline is optional when stdin ends. Empty input, 0, negative numbers, signs, leading zeros, multiple digits, extra words and overly long lines are rejected with an error message and a nonzero exit status. Later input lines are not used. This deliberately small format avoids conversion overflow and keeps number widths and alphabet indexing predictable.
For a square or triangle, n is the number of rows. Diamonds, hourglasses, butterflies, X shapes, plus signs and concentric squares use 2 * n - 1 rows; the butterfly is 2 * n characters wide. See each explanation before assuming that input 5 always produces five output lines. Every output uses ordinary spaces rather than tabs, has no trailing blanks, and ends with a newline. View it in a monospaced font.
If loops are new, first read the structure of a C program, then try Hello World. Each example separates input, a print_pattern function, and main; learn more about that separation in functions in C.
Star patterns
1. Solid square
For every row, print n stars. Both loops run from 1 through n, so the shape contains n * n stars and n newline characters. Start here before adding spaces or boundary tests.
Show C code: solid square
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int row = 1; row <= n; ++row) {
for (int col = 1; col <= n; ++col) putchar('*');
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
*****
*****
*****
*****
*****
2. Hollow square
Print a star on the first or last row, or the first or last column. All other cells are spaces. The border conditions overlap safely: for n = 1, the program prints one star, not four.
Show C code: hollow square
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int row = 1; row <= n; ++row) {
for (int col = 1; col <= n; ++col) {
putchar(row == 1 || row == n || col == 1 || col == n ? '*' : ' ');
}
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
*****
* *
* *
* *
*****
3. Left-aligned triangle
Row row contains exactly row stars. No leading spaces are needed. Unlike the square, the inner loop limit changes on every row: the total number of stars is n * (n + 1) / 2.
Show C code: left-aligned triangle
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int row = 1; row <= n; ++row) {
for (int col = 1; col <= row; ++col) putchar('*');
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Run left-aligned triangle online
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
*
**
***
****
*****
4. Inverted left-aligned triangle
Count the row width downward from n to 1. Print that many stars, then a newline. The left edge stays fixed while the right edge moves left; no spaces are required.
Show C code: inverted left-aligned triangle
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int width = n; width >= 1; --width) {
for (int col = 1; col <= width; ++col) putchar('*');
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Run inverted left-aligned triangle online
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
*****
****
***
**
*
5. Right-aligned triangle
Row row begins with n - row spaces and ends with row stars. Their sum is always n, which keeps the right edge fixed. Print the spaces before the stars, not after them.
Show C code: right-aligned triangle
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int row = 1; row <= n; ++row) {
for (int space = 0; space < n - row; ++space) putchar(' ');
for (int col = 1; col <= row; ++col) putchar('*');
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Run right-aligned triangle online
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
*
**
***
****
*****
6. Inverted right-aligned triangle
Let width decrease from n to 1. Print n - width leading spaces and width stars. The first row fills the entire width; each following row loses one star on its left.
Show C code: inverted right-aligned triangle
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int width = n; width >= 1; --width) {
for (int space = 0; space < n - width; ++space) putchar(' ');
for (int col = 1; col <= width; ++col) putchar('*');
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Run inverted right-aligned triangle online
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
*****
****
***
**
*
7. Centered star pyramid
Row row has n - row leading spaces followed by 2 * row - 1 stars. Odd star counts keep the tip centered over the base. Do not print spaces between stars in this compact version.
Show C code: centered star pyramid
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int row = 1; row <= n; ++row) {
for (int space = 0; space < n - row; ++space) putchar(' ');
for (int col = 1; col <= 2 * row - 1; ++col) putchar('*');
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Run centered star pyramid online
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
*
***
*****
*******
*********
8. Inverted centered pyramid
Use the same odd-width rule as the centered pyramid, but decrease level from n to 1. The leading-space count grows while the star count shrinks by two per row.
Show C code: inverted centered pyramid
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int level = n; level >= 1; --level) {
for (int space = 0; space < n - level; ++space) putchar(' ');
for (int col = 1; col <= 2 * level - 1; ++col) putchar('*');
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Run inverted centered pyramid online
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
*********
*******
*****
***
*
9. Hollow star pyramid
Use the centered pyramid dimensions. Within its odd-width row, print a star only on either edge or on the final row. The bottom is solid; the interior above it is hollow. A one-row pyramid is a single star.
Show C code: hollow star pyramid
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int row = 1; row <= n; ++row) {
for (int space = 0; space < n - row; ++space) putchar(' ');
for (int col = 1; col <= 2 * row - 1; ++col) {
putchar(col == 1 || col == 2 * row - 1 || row == n ? '*' : ' ');
}
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Run hollow star pyramid online
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
*
* *
* *
* *
*********
10. Solid diamond
Treat n as the height of the upper half including the middle. There are 2 * n - 1 rows. Convert each row to a level that rises from 1 to n, then falls to 1. This avoids printing the middle row twice.
Show C code: solid diamond
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int row = 1; row <= 2 * n - 1; ++row) {
int level = row <= n ? row : 2 * n - row;
for (int space = 0; space < n - level; ++space) putchar(' ');
for (int col = 1; col <= 2 * level - 1; ++col) putchar('*');
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
*
***
*****
*******
*********
*******
*****
***
*
11. Hollow diamond
Reuse the diamond level sequence, but print only the first and last star of each odd-width row. Unlike the hollow pyramid, the widest row is not solid: it is another pair of boundary stars.
Show C code: hollow diamond
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int row = 1; row <= 2 * n - 1; ++row) {
int level = row <= n ? row : 2 * n - row;
for (int space = 0; space < n - level; ++space) putchar(' ');
for (int col = 1; col <= 2 * level - 1; ++col) {
putchar(col == 1 || col == 2 * level - 1 ? '*' : ' ');
}
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
*
* *
* *
* *
* *
* *
* *
* *
*
12. Star hourglass
Start at level n, shrink to level 1, then grow back to n. Print one center row, not two. Each level has n - level leading spaces and 2 * level - 1 stars.
Show C code: star hourglass
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int row = 1; row <= 2 * n - 1; ++row) {
int level = row <= n ? n - row + 1 : row - n + 1;
for (int space = 0; space < n - level; ++space) putchar(' ');
for (int col = 1; col <= 2 * level - 1; ++col) putchar('*');
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
*********
*******
*****
***
*
***
*****
*******
*********
13. Solid butterfly
At each level, print level stars for the left wing, 2 * (n - level) spaces, and level stars for the right wing. Levels grow and then shrink without duplicating the center. The full width is 2 * n, and n = 1 prints two stars.
Show C code: solid butterfly
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int row = 1; row <= 2 * n - 1; ++row) {
int level = row <= n ? row : 2 * n - row;
for (int col = 1; col <= level; ++col) putchar('*');
for (int space = 0; space < 2 * (n - level); ++space) putchar(' ');
for (int col = 1; col <= level; ++col) putchar('*');
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *
14. Hollow butterfly
Keep the butterfly gap and level sequence, but print only the first and last star within each wing. At the center the wing edges touch; the interior of each wing remains hollow. For a wing of width 1, the two edge tests refer to the same cell.
Show C code: hollow butterfly
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int row = 1; row <= 2 * n - 1; ++row) {
int level = row <= n ? row : 2 * n - row;
for (int col = 1; col <= level; ++col) putchar(col == 1 || col == level ? '*' : ' ');
for (int space = 0; space < 2 * (n - level); ++space) putchar(' ');
for (int col = 1; col <= level; ++col) putchar(col == 1 || col == level ? '*' : ' ');
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
* *
** **
* * * *
* * * *
* ** *
* * * *
* * * *
** **
* *
15. X pattern
Use a conceptual square of side 2 * n - 1, indexed from zero. A star belongs on a diagonal when row == col or row + col == side - 1. Stop each row at its rightmost star to avoid trailing blanks; the omitted cells are only spaces.
Show C code: x pattern
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
int side = 2 * n - 1;
for (int row = 0; row < side; ++row) {
int other = side - 1 - row;
int last = row > other ? row : other;
for (int col = 0; col <= last; ++col) {
putchar(col == row || col == other ? '*' : ' ');
}
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
* *
* *
* *
* *
*
* *
* *
* *
* *
16. Plus pattern
The middle row is 2 * n - 1 stars. Every other row has n - 1 leading spaces followed by one star. The input sets the arm length including the center, so there are 2 * n - 1 rows.
Show C code: plus pattern
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int row = 1; row <= 2 * n - 1; ++row) {
if (row == n) {
for (int col = 1; col <= 2 * n - 1; ++col) putchar('*');
} else {
for (int space = 0; space < n - 1; ++space) putchar(' ');
putchar('*');
}
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
*
*
*
*
*********
*
*
*
*
Number patterns
17. Increasing number triangle
Print the column number from 1 through row. Each row restarts at 1. The 1–9 input bound ensures that every printed value here is a single decimal digit, so no separator is needed.
Show C code: increasing number triangle
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int row = 1; row <= n; ++row) {
for (int col = 1; col <= row; ++col) printf("%d", col);
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Run increasing number triangle online
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
1
12
123
1234
12345
18. Repeated-row number triangle
Print the row number row times. The outer loop chooses the value; the inner loop controls only its repetition. This differs from increasing numbers, where the inner loop chooses the value.
Show C code: repeated-row number triangle
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int row = 1; row <= n; ++row) {
for (int col = 1; col <= row; ++col) printf("%d", row);
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Run repeated-row number triangle online
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
1
22
333
4444
55555
19. Descending number triangle
Row row prints the values from row down to 1. The shape grows downward, but values decrease from left to right. Use a signed int for a loop that counts down to 1; do not accidentally make an unsigned zero wrap around.
Show C code: descending number triangle
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int row = 1; row <= n; ++row) {
for (int value = row; value >= 1; --value) printf("%d", value);
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Run descending number triangle online
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
1
21
321
4321
54321
20. Floyd triangle
Keep next outside both loops so counting continues across rows. Row row contains row consecutive integers. A field width of 3 keeps columns aligned through the largest allowed value, 45 at n = 9. These are leading padding spaces inside fixed-width number cells, not extra values.
Show C code: floyd triangle
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
int next = 1;
for (int row = 1; row <= n; ++row) {
for (int col = 1; col <= row; ++col) printf("%3d", next++);
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
21. Alternating binary triangle
Print 1 when row + col is even and 0 when it is odd. Both indices start at 1, so the top cell is 1. This checkerboard rule flips adjacent cells and the starting value of successive rows.
Show C code: alternating binary triangle
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int row = 1; row <= n; ++row) {
for (int col = 1; col <= row; ++col) printf("%d", (row + col) % 2 == 0);
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Run alternating binary triangle online
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
1
01
101
0101
10101
22. Palindromic number pyramid
Print n - row leading spaces, values 1 through row, then values row - 1 down to 1. Starting the descending half one lower avoids duplicating the peak digit. The bound of 9 keeps the compact pyramid aligned.
Show C code: palindromic number pyramid
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int row = 1; row <= n; ++row) {
for (int space = 0; space < n - row; ++space) putchar(' ');
for (int value = 1; value <= row; ++value) printf("%d", value);
for (int value = row - 1; value >= 1; --value) printf("%d", value);
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Run palindromic number pyramid online
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
1
121
12321
1234321
123454321
23. Pascal triangle
Rows are indexed from zero. Begin each row with coefficient 1, then compute the next coefficient using value * (row - col) / (col + 1). The division is exact for these binomial coefficients. Four-character fields and two leading spaces per missing cell form the display. The 1–9 bound keeps all intermediate arithmetic safe even when unsigned long is 32 bits; do not remove the bound without adding overflow checks.
Show C code: pascal triangle
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
for (int row = 0; row < n; ++row) {
for (int space = 0; space < 2 * (n - row - 1); ++space) putchar(' ');
unsigned long value = 1;
for (int col = 0; col <= row; ++col) {
printf("%4lu", value);
if (col < row) value = value * (unsigned long)(row - col) / (unsigned long)(col + 1);
}
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
24. Concentric number square
Build a square of side 2 * n - 1. Find the minimum distance from a cell to any outer edge; subtract that distance from n. The outside is n, the next ring is n - 1, and the center is 1. Print a separator before every cell except the first to avoid trailing spaces.
Show C code: concentric number square
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
int side = 2 * n - 1;
for (int row = 0; row < side; ++row) {
for (int col = 0; col < side; ++col) {
int distance = row < col ? row : col;
if (side - 1 - row < distance) distance = side - 1 - row;
if (side - 1 - col < distance) distance = side - 1 - col;
if (col > 0) putchar(' ');
printf("%d", n - distance);
}
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Run concentric number square online
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5
Alphabet patterns
25. Growing alphabet triangle
Use the literal alphabet string, then print its first row characters. Indexing a string avoids assuming that alphabet letters have consecutive numeric codes on every C implementation. Each row starts at A.
Show C code: growing alphabet triangle
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int row = 1; row <= n; ++row) {
for (int col = 0; col < row; ++col) putchar(alphabet[col]);
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Run growing alphabet triangle online
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
A
AB
ABC
ABCD
ABCDE
26. Repeated-row alphabet triangle
Choose alphabet[row - 1] once per row and repeat it row times. This is the alphabet version of the repeated-row number triangle, not a continuous sequence across the whole shape.
Show C code: repeated-row alphabet triangle
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int row = 1; row <= n; ++row) {
for (int col = 1; col <= row; ++col) putchar(alphabet[row - 1]);
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Run repeated-row alphabet triangle online
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
A
BB
CCC
DDDD
EEEEE
27. Inverted alphabet triangle
Start with the first n letters and shorten the row by one each time. Reset the alphabet index to zero on every row, so A remains at the left edge.
Show C code: inverted alphabet triangle
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int width = n; width >= 1; --width) {
for (int col = 0; col < width; ++col) putchar(alphabet[col]);
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Run inverted alphabet triangle online
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
ABCDE
ABCD
ABC
AB
A
28. Continuous alphabet triangle
Keep the letter index outside both loops. Continue across rows, wrapping from Z to A with remainder modulo 26. At the maximum input of 9 the shape contains 45 letters, so wrapping is necessary rather than reading beyond the string.
Show C code: continuous alphabet triangle
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int next = 0;
for (int row = 1; row <= n; ++row) {
for (int col = 1; col <= row; ++col) {
putchar(alphabet[next]);
next = (next + 1) % 26;
}
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Run continuous alphabet triangle online
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
A
BC
DEF
GHIJ
KLMNO
29. Palindromic alphabet pyramid
Print n - row leading spaces, letters A through the row letter, then the earlier letters in reverse. The center letter occurs once. For row 3 the letter sequence is ABCBA, not ABCCBA.
Show C code: palindromic alphabet pyramid
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int row = 1; row <= n; ++row) {
for (int space = 0; space < n - row; ++space) putchar(' ');
for (int col = 0; col < row; ++col) putchar(alphabet[col]);
for (int col = row - 2; col >= 0; --col) putchar(alphabet[col]);
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Run palindromic alphabet pyramid online
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
30. Hollow alphabet pyramid
The two edges use the row letter: A at the tip, B on row 2, and so on. Interior cells are spaces, except the base, which prints the palindrome A through the row letter and back to A. This combines a boundary test with a separate rule for the base; it is not simply the previous pattern with every letter removed.
Show C code: hollow alphabet pyramid
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* First line: one digit 1-9, optional whitespace, at most 63 characters. */
static int read_rows(int *rows) {
int ch, value = 0, length = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (++length > 63) return 0;
if (isspace((unsigned char)ch)) continue;
if (value != 0 || ch < '1' || ch > '9') return 0;
value = ch - '0';
}
if (ferror(stdin) || value == 0) return 0;
*rows = value;
return 1;
}
static void print_pattern(int n) {
const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int row = 1; row <= n; ++row) {
for (int space = 0; space < n - row; ++space) putchar(' ');
for (int col = 0; col < 2 * row - 1; ++col) {
if (row == n) {
int index = col < row ? col : 2 * row - 2 - col;
putchar(alphabet[index]);
} else {
putchar(col == 0 || col == 2 * row - 2 ? alphabet[row - 1] : ' ');
}
}
putchar('\n');
}
}
int main(void) {
int rows;
if (!read_rows(&rows)) {
fputs("Enter one digit from 1 to 9.\n", stderr);
return EXIT_FAILURE;
}
print_pattern(rows);
return EXIT_SUCCESS;
}
Run hollow alphabet pyramid online
Sample input
5
Expected output
Reference output for the displayed program, not a live execution result.
A
B B
C C
D D
ABCDEDCBA
How to derive a pattern instead of memorizing it
Start with a small output on paper. Number the rows and count three things: leading spaces, visible cells, and the value of each cell. Write those counts as expressions involving the current row. Only then translate them into loops.
For the centered pyramid with n = 5, use the following trace. A dot represents one leading space in the last column; the actual program prints spaces, not dots.
| Row | Leading spaces: n - row | Stars: 2 * row - 1 | Visible trace |
|---|---|---|---|
| 1 | 4 | 1 | ....* |
| 2 | 3 | 3 | ...*** |
| 3 | 2 | 5 | ..***** |
| 4 | 1 | 7 | .******* |
| 5 | 0 | 9 | ********* |
For hollow shapes, solve the solid shape first, then replace interior characters with spaces using a border condition. For mirrored shapes, derive one level that increases and decreases; this prevents accidental double middle rows. For number or alphabet patterns, separate the shape rule from the value rule: the same triangle can print a column index, row index or running counter.
Common mistakes and complexity
- One long line: put the newline after the inner loops, but inside the row loop.
- One character on every line: do not place the newline inside the character loop.
- Shifted pyramid: count leading spaces independently from stars. Tabs do not have a fixed one-column width.
- Repeated middle row: diamonds and butterflies here have
2 * n - 1rows, not2 * n. - Letters after Z: use the alphabet string and an explicit modulo-26 wrap for the continuous sequence. Do not index beyond the 26 letters.
- Misaligned numbers: compact digit patterns are intentionally capped at 9. Floyd and Pascal use fixed-width numeric fields instead. Increasing the row limit also requires a fresh width and arithmetic-safety review.
- Unexpected invalid-input message: enter a single digit such as
5, notrows = 5,05or+5. The small format is deliberate, not a full integer parser.
If the parameter were allowed to grow, these implementations would take Theta(n squared) time in emitted characters, including spaces. Even a sparse X or plus sign prints a quadratic amount of spacing across its rows. They use O(1) auxiliary space: no growing arrays or recursion are needed. The fixed alphabet table is constant storage. The actual programs cap n at 9, so their work is bounded. Generalizing Pascal to many rows changes the arithmetic problem; constant-size integers eventually overflow.
Practice with answers
Exercise 1: remove the repeated peak
A number pyramid row is 123321, but the target is 12321. Which loop bound is wrong?
Answer: the descending loop must start at row - 1, not row. The ascending half already printed the peak. Compare the palindromic number pyramid.
Exercise 2: reverse a triangle without adding spaces
Turn a left-aligned triangle into an inverted left-aligned triangle while keeping the same left edge. What changes?
Answer: decrease the row width from n to 1 instead of increasing it. Keep printing a newline after each row and add no leading-space loop. Run the inverted left-aligned triangle with input 3: its row lengths are 3, 2 and 1.
Exercise 3: predict the boundary case
With input 1, how do the hollow diamond and solid butterfly differ?
Answer: the hollow diamond prints * and a newline; the solid butterfly prints ** and a newline because its width is 2 * n. This is why boundary tests should follow the stated dimensions rather than a guessed picture.
Exercise 4: find the first wrapped alphabet row
Which row of the continuous alphabet triangle first wraps from Z to A, and what does it print?
Answer: rows 1 through 6 use 1 + 2 + 3 + 4 + 5 + 6 = 21 letters. Row 7 prints VWXYZAB. Run the continuous alphabet triangle with input 7 to check it.
Frequently asked questions
Can I use while loops instead of for loops?
Yes. Move the initializer before the loop, retain the condition, and put the increment or decrement at the end of the body. Reset each inner-loop counter for every row. The space and character formulas do not change. The complete examples here use for loops to keep those three parts together.
Why do the programs reject 10 or a negative row count?
The 1–9 limit is part of the input contract, not a limitation of C. It keeps these teaching examples short, safely indexed and aligned. A larger general-purpose version needs numeric parsing, a considered upper bound, wider output formatting and overflow checks where arithmetic grows. Do not merely remove the check.
Is Pascal triangle safe for unlimited rows?
No. The bounded example uses unsigned long and checks the row range before doing arithmetic. For larger rows, even the intermediate multiplication in the recurrence can exceed the type's range. Unsigned wraparound is defined by C, but it gives incorrect coefficients. Use checked arithmetic or an appropriate arbitrary-precision approach for a larger implementation.
Are these graphics programs?
No. They print text characters to stdout. There are no graphics libraries, screen-position commands or platform-specific console calls. A monospaced text display preserves the spacing. The expected outputs are reference data; the live compiler's result appears only after you run the code.
What should I practice next?
Use C practice questions for more exercises. Move from output patterns to arrays, linear search and insertion sort to practice loops that transform or inspect data rather than draw shapes.