C++ Number Pattern Programs

Beginner
6 min read
Updated: Nov 2026
3 examples
int + cout

Definition and Usage

A C++ number pattern is a console program that prints integers in rows. The outer for loop chooses the row. The inner for loop prints each int with cout << n, then cout << '\n' ends that row.

cout << n prints the decimal digits and stays on the line. Single digits sit next to each other (123). Once a value reaches 10, stream a space or set a width with setw so 10 and 11 do not become 1011.

The loop variable is the number.

Print col for 1, 2, 3… Print row to repeat the same digit. A separate int value that you increment is how running sequences cross from one row to the next.

C++ Syntax

Count the outer loop down when the first row should be the longest. The inner loop still walks 1 through the current width.

C++
#include <iostream>
using namespace std;

for (int row = rows; row >= 1; row--) {
    for (int n = 1; n <= row; n++)
        cout << n;     // digits, no space
    cout << '\n';    // end the row
}

How C++ Prints a Number

Map each part of the console shape to a C++ type or stream insertion.

PieceC++What it does
Digitcout << nPrints the int with no extra space
Same digitcout << rowRepeats the row number on that line
Next valuevalue++Keeps counting across columns and rows
New rowcout << '\n'Finish the row
Widthcout << setw(2) << nTwo columns on the next insertion only
Odd or evenn % 20/1 patterns from the remainder
One digitstatic_cast<char>('0' + n)One character for 0–9

C++ Examples

Three programs. Each one changes only the C++ that chooses the int or its width.

Example 1 — Shrink the row with the outer loop

Start row at 5 and stop at 1. The inner loop prints 1 through row.

C++
#include <iostream>
using namespace std;

int main() {
    int rows = 5;

    for (int row = rows; row >= 1; row--) {
        for (int n = 1; n <= row; n++)
            cout << n;
        cout << '\n';
    }
    return 0;
}

How It Works

  1. row-- makes each later line one digit shorter.
  2. The inner for always restarts at 1, so every row begins with 1.
  3. cout << n prints the int; cout << '\n' ends the row.

Example 2 — Print the row number, not the column

The inner loop only decides how many times to print. The value is row.

C++
#include <iostream>
using namespace std;

int main() {
    int rows = 5;

    for (int row = 1; row <= rows; row++) {
        for (int col = 1; col <= row; col++)
            cout << row;
        cout << '\n';
    }
    return 0;
}

How It Works

  1. Row 1 runs the inner loop once and prints 1.
  2. Row 3 runs it three times and prints 3 each time.
  3. Swap cout << row for cout << col and the line becomes 123.

Example 3 — Keep two-digit values aligned

setw(2) reserves two columns for the next insertion. The following ' ' separates columns.

C++
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int value = 1;

    for (int row = 1; row <= 4; row++) {
        for (int col = 1; col <= row; col++) {
            cout << setw(2) << value << ' ';
            value++;
        }
        cout << '\n';
    }
    return 0;
}

How It Works

  1. value lives outside both loops, so counting continues on the next row.
  2. setw(2) applies only to value, so 1 is two columns and 10 stays two digits.
  3. The ' ' after value is a separate insertion, so the width does not pad the space.

62 C++ Number Pattern Programs

Each card is a full C++ program: nested for loops, an int for the cell, and cout << n. Open one for the walkthrough.

Number Pattern 1
Number Pattern 2
Number Pattern 3
Number Pattern 4
Number Pattern 5
Number Pattern 6
Number Pattern 7
Number Pattern 8
Number Pattern 9
Number Pattern 10
Number Pattern 11
Number Pattern 12
Number Pattern 13
Number Pattern 14
Number Pattern 15
Number Pattern 16
Number Pattern 17
Number Pattern 18
Number Pattern 19
Number Pattern 20
Number Pattern 21
Number Pattern 22
Number Pattern 23
Number Pattern 24
Number Pattern 25
Number Pattern 26
Number Pattern 27
Number Pattern 28
Number Pattern 29
Number Pattern 30
Number Pattern 31
Number Pattern 32
Number Pattern 33
Number Pattern 34
Number Pattern 35
Number Pattern 36
Number Pattern 37
Number Pattern 38
Number Pattern 39
Number Pattern 40
Number Pattern 41
Number Pattern 42
Number Pattern 43
Number Pattern 44
Number Pattern 45
Number Pattern 46
Number Pattern 47
Number Pattern 48
Number Pattern 49
Number Pattern 50
Number Pattern 51
Number Pattern 52
Number Pattern 53
Number Pattern 54
Number Pattern 55
Number Pattern 56
Number Pattern 57
Number Pattern 58
Number Pattern 59
Number Pattern 60
Number Pattern 61
Number Pattern 62

Frequently Asked Questions

cout << 10 prints the characters 1 and 0 and stays on the line. The next insertion starts immediately, so cout << 10 << 11 becomes 1011. Stream a space, or set a field width with setw, when values can be more than one digit.
cout << n prints the int and stays on the same line. cout << '\n' ends the row. Call it once after the inner loop. cout << endl also ends the row and flushes the stream.
Include <iomanip> and call setw(2) before the number. setw applies only to the next insertion, so cout << setw(2) << n << ' ' pads n to two columns and then prints a separating space. The width does not stick for the space.
Yes, when the value is 0 through 9. cout << static_cast<char>('0' + n) writes one character. cout << n still works, and it is the insertion you need once the value reaches 10.

Next: Number Pattern 1

A full C++ walkthrough of the descending number triangle, from the outer for through cout << '\n'.

Number Pattern 1 →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

28 people found this page helpful