C++ Program to Print Triangle, Reverse Pyramid, and Diamond Patterns using stars

In this program, we will learn how to print Triangle, Reverse Pyramid, and Diamond patterns using stars in C++ programming language.

C++ Programming Code to Print Triangle Pattern using stars

Code:-

#include <iostream>

using namespace std;

int main(){
    int rows, space;

    cout << "Enter the number of rows: ";
    cin >> rows;

    space = rows - 1;

    for (int i = 1; i<=rows; i++){
        for (int j = 1; j<=space; j++)
            cout << "  ";

        --space;

        for (int k = 1; k<= 2*i-1; k++)
            cout << "* ";

        cout << endl;
    }
    return 0;
}

Output:-

Enter the number of rows: 5
        * 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * * 

C++ Program to Print Reverse Pyramid Pattern using stars

Code:-

#include <iostream>

using namespace std;

int main(){
    int rows, space;

    cout<<"Enter the number of rows: ";
    cin>>rows;

    space = 0;

    for (int i = 0; i < rows; i++){
        for (int j = 1; j<= space; j++)
            cout << "  ";

        space++;

        for (int k = 1 ; k <= 2*(rows-i)-1; k++)
            cout << "* ";

        cout << endl;
    }


  return 0;
}

Output:-

Enter the number of rows: 5
* * * * * * * * * 
  * * * * * * * 
    * * * * * 
      * * * 
        * 

C++ Program to Print Diamond Pattern using stars

Code:-

#include <iostream>

using namespace std;

int main(){
    int rows, space;

    cout << "Enter the number of rows: ";
    cin >> rows;

    space = rows - 1;

    for (int i = 1; i<=rows; i++){
        for (int j = 1; j<=space; j++)
            cout << "  ";

        --space;

        for (int k = 1; k<= 2*i-1; k++)
            cout << "* ";

        cout << endl;
    }

    space = 1;

    for (int i = 1; i<= rows - 1; i++){
        for (int j = 1; j<= space; j++)
            cout << "  ";

        space++;

        for (int k = 1 ; k<= 2*(rows-i)-1; k++)
            cout << "* ";

        cout << endl;
    }

    return 0;
}

Output:-

Enter the number of rows: 5
        * 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * * 
  * * * * * * * 
    * * * * * 
      * * * 
        * 

You can learn about many other C++ Programs Here.

Best Books for learning C++ programming language with Data Structure and Algorithms.

The following two tabs change content below.

Amit Rawat

Founder and Developer at SpiderLabWeb
I love to work on new projects and also work on my ideas. My main field of interest is Web Development.

You may also like...