Star Pattern

Star Pattern in C++ (15 Programs With Output)

IntermediateTopic: Advanced Pattern Programs
Back

C++ Star 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 Half Pyramid
    cout << "\nRight Half Pyramid:" << endl;
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= i; j++) {
            cout << "* ";
        }
        cout << endl;
    }
    
    // Left Half Pyramid
    cout << "\nLeft Half Pyramid:" << endl;
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= rows - i; j++) {
            cout << "  ";
        }
        for (int j = 1; j <= i; j++) {
            cout << "* ";
        }
        cout << endl;
    }
    
    // Full Pyramid
    cout << "\nFull Pyramid:" << endl;
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= rows - i; j++) {
            cout << " ";
        }
        for (int j = 1; j <= 2 * i - 1; j++) {
            cout << "*";
        }
        cout << endl;
    }
    
    return 0;
}
Output
Enter number of rows: 5

Right Half Pyramid:
*
* *
* * *
* * * *
* * * * *

Left Half Pyramid:
        *
      * *
    * * *
  * * * *
* * * * *

Full Pyramid:
    *
   ***
  *****
 *******
*********

Understanding Star Pattern

This program demonstrates 15 different star patterns: right half pyramid, left half pyramid, full pyramid, inverted pyramid, diamond, hollow pyramid, hourglass, arrow, cross, plus, square, rectangle, triangle variations, and more.

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