Triangle Pattern

Triangle Pattern in C++ (10 Programs)

IntermediateTopic: Advanced Pattern Programs
Back

C++ Triangle 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;
    
    // Right Triangle
    cout << "\nRight Triangle:" << endl;
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= i; j++) {
            cout << "* ";
        }
        cout << endl;
    }
    
    // Inverted Right Triangle
    cout << "\nInverted Right Triangle:" << endl;
    for (int i = rows; i >= 1; i--) {
        for (int j = 1; j <= i; j++) {
            cout << "* ";
        }
        cout << endl;
    }
    
    // Hollow Triangle
    cout << "\nHollow Triangle:" << endl;
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= i; j++) {
            if (j == 1 || j == i || i == rows) {
                cout << "* ";
            } else {
                cout << "  ";
            }
        }
        cout << endl;
    }
    
    return 0;
}
Output
Enter number of rows: 5

Right Triangle:
*
* *
* * *
* * * *
* * * * *

Inverted Right Triangle:
* * * * *
* * * *
* * *
* *
*

Hollow Triangle:
*
* *
*   *
*     *
* * * * *

Understanding Triangle Pattern

This program demonstrates 10 different triangle patterns: right triangle, inverted triangle, hollow triangle, mirrored triangle, number triangle, alphabet triangle, 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