Number Pattern

C++ Program To Print Number Pattern (10 Ways With Output)

IntermediateTopic: Advanced 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;
    
    // 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

Understanding Number Pattern

This program demonstrates 10 different number patterns: increasing numbers, same number in row, decreasing numbers, Floyd's triangle, Pascal's triangle, number pyramid, hollow number patterns, and various combinations.

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.

Table of Contents