Number Pattern
Program to print number pattern
C++ Number Pattern Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}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:
---
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
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:
11 21 2 31 2 3 41 2 3 4 5---
6. Step-by-Step Breakdown
Let's trace through when rows = 5:
Row 1 (i = 1):
i = 1j runs from 1 to 1j = 1: prints 1 cout << endl; moves to next line1Row 2 (i = 2):
i = 2j runs from 1 to 2j = 1: prints 1 j = 2: prints 2 1 2Row 3 (i = 3):
j runs from 1 to 31 2 3Row 4 (i = 4):
j runs from 1 to 41 2 3 4Row 5 (i = 5):
j runs from 1 to 51 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`)
:
i goes from 1 to rowsInner Loop (`j`)
:
j <= ii, print numbers from 1 to ij is what we print`cout << j << " ";`
:
j (the number)`cout << endl;`
:
---
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 jVariation 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:
---
Summary
i) controls which row we're printing (1 to rows).j) runs from 1 to i, printing each number.j (not i) to get sequential numbers 1, 2, 3, 4...endl moves to the next line.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.