Number Pattern

Program to print number pattern

BeginnerTopic: Pattern Programs
Back

C++ Number Pattern Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
using namespace std;

int main() {
    int rows;
    
    cout << "Enter number of rows: ";
    cin >> rows;
    
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= i; j++) {
            cout << j << " ";
        }
        cout << endl;
    }
    
    return 0;
}
Output
Enter number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Understanding Number Pattern

This program teaches you how to print a number pattern using nested loops in C++. Instead of printing stars, this pattern prints numbers in a sequential order. Each row displays numbers from 1 up to the row number, creating a triangular number pattern. This is an excellent way to understand how to use loop variables in output and transition from character patterns to number patterns.

---

1. What is a Number Pattern?

A number pattern looks like this (for 5 rows):

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Notice:

Each row starts from 1
Each row ends at the row number
Row 1 has numbers 1
Row 2 has numbers 1, 2
Row 3 has numbers 1, 2, 3
And so on...

---

2. Header File

#include <iostream>

Provides cout for output and cin for input.

---

3. Variable Declaration

int rows;

Stores the number of rows the user wants in the pattern.

---

4. Taking Input

`cout << "Enter number of rows: ";`

cin >> rows;

Gets the number of rows from the user.

---

5. Understanding the Pattern Logic

The key insight is:

In row i, we print numbers from 1 to i.

This means:

Row 1: Print 1 to 1 → 1
Row 2: Print 1 to 2 → 1 2
Row 3: Print 1 to 3 → 1 2 3
Row 4: Print 1 to 4 → 1 2 3 4
Row 5: Print 1 to 5 → 1 2 3 4 5

---

6. Step-by-Step Breakdown

Let's trace through when rows = 5:

Row 1 (i = 1):

Outer loop: i = 1
Inner loop: j runs from 1 to 1
j = 1: prints 1
After inner loop, cout << endl; moves to next line
Output: 1

Row 2 (i = 2):

Outer loop: i = 2
Inner loop: j runs from 1 to 2
j = 1: prints 1
j = 2: prints 2
Output: 1 2

Row 3 (i = 3):

Inner loop: j runs from 1 to 3
Prints: 1 2 3

Row 4 (i = 4):

Inner loop: j runs from 1 to 4
Prints: 1 2 3 4

Row 5 (i = 5):

Inner loop: j runs from 1 to 5
Prints: 1 2 3 4 5

---

7. Code Explanation

for (int i = 1; i <= rows; i++) {
    for (int j = 1; j <= i; j++) {
    }

    cout << endl;
}
        cout << j << " ";

Outer Loop (`i`)

:

Controls which row we're printing
i goes from 1 to rows

Inner Loop (`j`)

:

Condition: j <= i
This means: in row i, print numbers from 1 to i
The loop variable j is what we print

`cout << j << " ";`

:

Prints the current value of j (the number)
Adds a space after each number for readability

`cout << endl;`

:

Moves to the next line after printing all numbers in the current row

---

8. Key Difference: Printing j vs Printing i

This is an important concept:

-

`cout << j;`

→ Prints the inner loop variable (1, 2, 3, 4, 5...)

-

`cout << i;`

→ Would print the outer loop variable (1, 2, 2, 2, 3, 3, 3, 3...)

Using j gives us the sequential number pattern we want.

---

9. Visual Representation

For `rows = 5`:

Row 1: j values [1]           → 1
Row 2: j values [1, 2]        → 1 2
Row 3: j values [1, 2, 3]     → 1 2 3
Row 4: j values [1, 2, 3, 4]  → 1 2 3 4
Row 5: j values [1, 2, 3, 4, 5] → 1 2 3 4 5

---

10. Common Variations

Once you understand this pattern, you can create variations:

Variation 1: Same number in each row

Output:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
cout << i << " ";  // Print i instead of j

Variation 2: Reverse numbers

Output:

1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
cout << (i - j + 1) << " ";

Variation 3: Continuous numbers (Floyd's Triangle)

int num = 1;
for (int i = 1; i <= rows; i++) {
    for (int j = 1; j <= i; j++) {
    }

    cout << endl;
}

Output:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

---

        cout << num++ << " ";

11. Key Concepts Demonstrated

1.

Using Loop Variables in Output

: The inner loop variable j is directly used in the output, which is a powerful technique.

2.

Sequential Number Printing

: Understanding how to print numbers in sequence within nested loops.

3.

Pattern Recognition

: Recognizing that row i should contain numbers 1 to i.

4.

Transition from Characters to Numbers

: This bridges the gap between printing characters (like *) and printing numeric values.

---

12. Common Mistakes

1.

Printing i instead of j

: Would create a different pattern (same number repeated)

2.

Wrong loop condition

: Using j <= rows instead of j <= i

3.

Missing space

: Not adding " " after j makes numbers run together

4.

Wrong starting value

: Starting j from 0 instead of 1

---

13. Practical Applications

Number patterns are used in:

Educational programs (teaching counting, sequences)
Mathematical visualizations
Game development (level indicators, score displays)
Data representation (showing sequences, progressions)
Algorithm visualization (showing how loops work)

---

Summary

The outer loop (i) controls which row we're printing (1 to rows).
The inner loop (j) runs from 1 to i, printing each number.
We print j (not i) to get sequential numbers 1, 2, 3, 4...
After each row, endl moves to the next line.
This creates a number pattern where each row contains numbers from 1 to the row number.

This program is essential for understanding how to use loop variables in output and creating numeric patterns. It's a foundation for more complex number patterns and mathematical visualizations.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for Number Pattern

This C++ program is part of the "Pattern Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents