#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;
}Output
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
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:
- Pattern 1: Increasing numbers (1, 1 2, 1 2 3, ...)
- Pattern 2: Same number in row (1, 2 2, 3 3 3, ...)
Number patterns demonstrate how to control the printing of numbers to form visual sequences and shapes.
2. Header File Used
#include <iostream>
This header provides:
- cout for displaying output
- cin for taking input from the user
3. Understanding Number Patterns
Key Concepts:
- Numbers replace stars in pattern printing
- Numbers can follow different sequences
- Outer loop controls rows
- Inner loop controls which numbers to print
Pattern Types:
- Increasing: numbers increase within and across rows
- Same Number: all numbers in a row are the same
- Decreasing: numbers decrease
- Sequential: continuous number sequences
4. Declaring Variables
The program declares: int rows;
- rows stores the number of rows entered by the user.
- This determines the size of all number patterns.
5. Taking Input From the User
The program asks: cout << "Enter number of rows: "; cin >> rows;
The user enters a number, for example: 5
6. Pattern 1: Increasing Numbers
for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { cout << j << " "; } cout << endl; }
How it works:
- Outer loop (i): iterates from 1 to rows (each row)
- Inner loop (j): prints numbers from 1 to i
- Row 1: prints 1
- Row 2: prints 1 2
- Row 3: prints 1 2 3
- Each row starts from 1 and goes up to row number
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 << i << " "; } cout << endl; }
How it works:
- Outer loop (i): iterates from 1 to rows
- Inner loop (j): prints the row number (i) repeatedly
- Row 1: prints 1 (one time)
- Row 2: prints 2 (two times)
- Row 3: prints 3 (three times)
- All numbers in a row are the same (equal to row number)
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):
- Inner loop variable j is printed
- j goes from 1 to i
- Creates sequence: 1, 1 2, 1 2 3, ...
Pattern 2 (Same Number):
- Row number i is printed
- Printed i times (j from 1 to i)
- Creates: 1, 2 2, 3 3 3, ...
Key Difference:
- Pattern 1: print j (changes in inner loop)
- Pattern 2: print i (constant in inner loop)
9. Other Patterns (Mentioned but not shown in code)
Decreasing Numbers:
- Print numbers in decreasing order
- Example: 5, 4 3, 3 2 1, ...
Floyd's Triangle:
- Continuous sequence: 1, 2 3, 4 5 6, 7 8 9 10, ...
- Requires counter variable
Pascal's Triangle:
- Mathematical pattern with binomial coefficients
- More complex calculation
Number Pyramid:
- Numbers arranged in pyramid shape
- Combines number pattern with spacing
Hollow Number Patterns:
- Numbers only at edges
- Uses conditional printing
10. When to Use Number Patterns
Educational Purposes:
- Learning nested loops with numbers
- Understanding number sequences
- Developing pattern recognition
Interview Preparation:
- Common coding interview problem
- Tests understanding of loops and sequences
- Demonstrates logical thinking
Mathematical Applications:
- Visualizing number sequences
- Understanding mathematical patterns
- Educational demonstrations
11. Important Considerations
Number Selection:
- Which number to print: j, i, or calculated value?
- Depends on desired pattern
- Understand the difference
Spacing:
- " " (space) after each number for readability
- Affects pattern appearance
- Consider formatting
Sequence Logic:
- Increasing: use inner loop variable
- Same: use outer loop variable
- Custom: calculate based on position
12. return 0;
This ends the program successfully.
Summary
- Number patterns replace stars with numbers in pattern printing.
- Pattern 1 (Increasing): print j from 1 to i, creates 1, 1 2, 1 2 3, ...
- Pattern 2 (Same Number): print i repeated i times, creates 1, 2 2, 3 3 3, ...
- Key difference: print inner loop variable (j) vs outer loop variable (i).
- Understanding which variable to print is essential.
- Multiple variations exist: decreasing, Floyd's, Pascal's, pyramid.
- Number patterns develop understanding of sequences and loops.
This program is fundamental for beginners learning number sequences, understanding nested loops with numbers, and preparing for more complex pattern problems in C++ programs.