0
0
CppProgramBeginner · 2 min read

C++ Program to Print Number Pattern

You can print a number pattern in C++ using nested for loops; for example, for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { std::cout << j << ' '; } std::cout << '\n'; } prints numbers from 1 up to the current line number.
📋

Examples

Input3
Output1 1 2 1 2 3
Input5
Output1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Input1
Output1
🧠

How to Think About It

To print a number pattern, think of rows and columns. Each row number tells how many numbers to print. Use one loop to go through each row, and inside it, another loop to print numbers from 1 up to the current row number.
📐

Algorithm

1
Get the number of rows (n) from the user.
2
Start a loop from 1 to n for each row.
3
Inside this loop, start another loop from 1 to the current row number.
4
Print the inner loop number followed by a space.
5
After the inner loop ends, print a newline to move to the next row.
6
Repeat until all rows are printed.
💻

Code

cpp
#include <iostream>

int main() {
    int n;
    std::cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            std::cout << j << ' ';
        }
        std::cout << '\n';
    }
    return 0;
}
Output
1 1 2 1 2 3
🔍

Dry Run

Let's trace input 3 through the code

1

Input read

n = 3

2

First outer loop iteration (i=1)

Inner loop runs j=1; prints '1 '

3

Print newline after first row

Output: '1 \n'

4

Second outer loop iteration (i=2)

Inner loop runs j=1,2; prints '1 2 '

5

Print newline after second row

Output: '1 \n1 2 \n'

6

Third outer loop iteration (i=3)

Inner loop runs j=1,2,3; prints '1 2 3 '

7

Print newline after third row

Output: '1 \n1 2 \n1 2 3 \n'

i (row)j (column)Printed Number
111
211
222
311
322
333
💡

Why This Works

Step 1: Outer loop controls rows

The outer for loop runs from 1 to n, each iteration representing one row of the pattern.

Step 2: Inner loop prints numbers

The inner for loop runs from 1 to the current row number, printing numbers in increasing order.

Step 3: Newline after each row

After printing numbers for a row, std::cout << '\n' moves the cursor to the next line for the next row.

🔄

Alternative Approaches

Reverse number pattern
cpp
#include <iostream>

int main() {
    int n;
    std::cin >> n;
    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= i; j++) {
            std::cout << j << ' ';
        }
        std::cout << '\n';
    }
    return 0;
}
Prints numbers starting from n down to 1 rows, showing a decreasing pattern.
Print same number in each row
cpp
#include <iostream>

int main() {
    int n;
    std::cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            std::cout << i << ' ';
        }
        std::cout << '\n';
    }
    return 0;
}
Prints the row number repeatedly in each row instead of counting up.

Complexity: O(n^2) time, O(1) space

Time Complexity

The nested loops run approximately n*(n+1)/2 times, which is O(n^2), because for each row i, the inner loop runs i times.

Space Complexity

The program uses constant extra space, O(1), as it only stores loop counters and prints output directly.

Which Approach is Fastest?

All approaches use nested loops with similar time complexity; differences are mainly in output style, not performance.

ApproachTimeSpaceBest For
Counting up patternO(n^2)O(1)Simple increasing number patterns
Reverse number patternO(n^2)O(1)Decreasing rows pattern
Same number per rowO(n^2)O(1)Patterns with repeated numbers per row
💡
Use nested loops where the outer loop controls rows and the inner loop controls columns to print patterns.
⚠️
Beginners often forget to print a newline after each row, causing all output to appear on one line.