Number Pattern
C++ Program To Print Number Pattern (10 Ways With Output)
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;
// Pattern 1: Increasing numbers
cout << "\nPattern 1:" << endl;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
cout << j << " ";
}
cout << endl;
}
// Pattern 2: Same number in row
cout << "\nPattern 2:" << endl;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
cout << i << " ";
}
cout << endl;
}
// Pattern 3: Decreasing numbers
cout << "\nPattern 3:" << endl;
for (int i = 1; i <= rows; i++) {
for (int j = i; j >= 1; j--) {
cout << j << " ";
}
cout << endl;
}
// Pattern 4: Floyd's Triangle
cout << "\nPattern 4 (Floyd's Triangle):" << endl;
int num = 1;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
cout << num++ << " ";
}
cout << endl;
}
return 0;
}Enter number of rows: 5 Pattern 1: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 Pattern 2: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Pattern 3: 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 Pattern 4 (Floyd's Triangle): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Understanding Number Pattern
This program teaches you how to print various number patterns in C++ using nested loops. Number patterns replace stars with numbers, creating sequences that follow mathematical rules. These patterns help understand number sequences, loop control, and pattern formation, and are commonly used in programming education and interviews.
---
1. What This Program Does
The program prints different number patterns based on the number of rows entered by the user. For example, with 5 rows, it creates:
Number patterns demonstrate how to control the printing of numbers to form visual sequences and shapes.
---
2. Header File Used
This header provides:
---
#include <iostream>3. Understanding Number Patterns
Key Concepts
:
Pattern Types
:
---
4. Declaring Variables
The program declares:
int rows;
---
5. Taking Input From the User
The program asks:
cin >> rows;
---
cout << "Enter number of rows: ";6. Pattern 1: Increasing Numbers
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
}
cout << endl;
}
cout << j << " ";How it works
:
Output
(for rows = 5):
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
---
7. Pattern 2: Same Number in Row
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
}
cout << endl;
}
cout << i << " ";How it works
:
Output
(for rows = 5):
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
---
8. Understanding the Patterns
Pattern 1 (Increasing)
:
Pattern 2 (Same Number)
:
Key Difference
:
---
9. Other Patterns (Mentioned but not shown in code)
Decreasing Numbers
:
Floyd's Triangle
:
Pascal's Triangle
:
Number Pyramid
:
Hollow Number Patterns
:
---
10. When to Use Number Patterns
Educational Purposes
:
Interview Preparation
:
Mathematical Applications
:
---
11. Important Considerations
Number Selection
:
Spacing
:
Sequence Logic
:
---
12. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for beginners learning number sequences, understanding nested loops with numbers, and preparing for more complex pattern problems in C++ programs.
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 "Advanced 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.