C++ program to print star patterns

Star pattern programs are very popular in programming languages to understand looping concepts. In this article, we will learn the C++ programs to print star patterns using steric and loops.

Example 1: C++ program to print triangle or half pyramid

Triangle or a half pyramid is actually a right-angle triangle constricted using the steric symbol. In this program take the maximum number of rows from the user and display a triangle or half pyramid on the console.

Code:

//C++ program to print triangle or half pyramid
#include <iostream>
using namespace std;
int main()
{
int  i, j, rows;
cout << "Enter total number of rows:  ";
cin >> rows;
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= i; j++)
{
cout << "*";
}
cout << "\n";
}
return 0;
}

Output

Enter total number of rows:  5
*
**
***
****
*****

Example 2: C++ program to print inverted triangle or half pyramid

In this program just inverted triangle or half pyramid downward.  Below is the program to display it on the screen.

//C++ program to print inverted triangle or half pyramid
#include <iostream>
using namespace std;
int main()
{
int  i, j, rows;
cout << "Enter total number of rows:  ";
cin >> rows;
for(i = rows; i >= 1; i--)
{
for(j = 1; j <= i; j++)
{
cout << "*";
}
cout << "\n";
}
return 0;
}

Output

Enter total number of rows:  5
*****
****
***
**
*

Example 3: C++ program to print full pyramid

Bellow program helps you to display the full pyramid on the screen. Its design looks like historical Egyptian pyramids.

//C++ program to print full pyramid
#include<iostream>
using namespace std;
int main()
{
int i, j, rows, space;
cout << "Enter number of rows: ";
cin >> rows;
for(i = 1; i <= rows; i++)
{
for(space = i; space < rows; space++)
{
cout << " ";
}
for(j = 1; j <= (2 * i - 1); j++)
{
cout << "*";
}
cout << "\n";
}
}

Output

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

Example 4: C++ program to print inverted full pyramid

This program displays a full pyramid just inverted downward.

//C++ Program to print inverted full pyramid
#include<iostream>
using namespace std;
int main()
{
int i,j, rows, space;
cout << "Enter total number of rows: ";
cin >> rows;
for(i = rows; i >= 1; i--)
{
for(space = i; space < rows; space++)
cout << " ";
for(j = 1; j <= (2 * i - 1); j++)
cout << "*";
cout << "\n";
}
return 0;
}

Output

Enter total number of rows: 5
*********
 *******
  *****
   ***
    *

Example 5: C++ program to print diamond pattern

This program displays a rhombus-shaped diamond pattern.

//C++ program to print diamond pattern
#include<iostream>
using namespace std;
int main()
{
int i, j, rows, space;
cout << "Enter number of rows: ";
cin >> rows;
for(i = 0; i <= rows; i++)
{
for(space = rows; space > i; space--)
cout << " ";
for(j=0; j<i; j++)
cout << "* ";
cout << "\n";
}
for(i = 1; i < rows; i++)
{
for(space = 0; space < i; space++)
cout << " ";
for(j = rows; j > i; j--)
cout << "* ";
cout << "\n";
}
return 0;
}

Output

Enter number of rows: 5

    *
   * *
  * * *
 * * * *
* * * * *
 * * * *
  * * *
   * *
    *

Hope you understand the C++ program to print star patterns. You also check the following related programs.

  1. C++ program to print Pascal triangle
  2. C++ program to print Floyd triangle