C++ Program To Print The Diamond Shape

Last Updated : 18 Aug, 2026

A diamond pattern in C++ can be printed by combining increasing and decreasing star patterns using nested loops. The pattern is formed by controlling the number of spaces and stars in each row.

  • The upper half increases the number of stars from 1 to n.
  • The lower half decreases the number of stars from n - 1 to 1.

Illustration

Input: 5

Output:

C++ Program to Print the Diamond Shape

C++
#include <iostream>
using namespace std;

void printDiamond(int n)
{
    // Print the upper half
    for (int i = 1; i <= n; i++)
    {
        // Print leading spaces
        for (int j = 1; j <= n - i; j++)
            cout << " ";

        // Print stars
        for (int j = 1; j <= i; j++)
            cout << "* ";

        cout << endl;
    }

    // Print the lower half
    for (int i = n - 1; i >= 1; i--)
    {
        // Print leading spaces
        for (int j = 1; j <= n - i; j++)
            cout << " ";

        // Print stars
        for (int j = 1; j <= i; j++)
            cout << "* ";

        cout << endl;
    }
}

int main()
{
    int n = 5;

    printDiamond(n);

    return 0;
}
 

Output
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
 * * * *
  * * *
   * *
    *

Explanation: For n = 5, the upper half prints 1, 2, 3, 4, and 5 stars. The lower half then prints 4, 3, 2, and 1 stars. For each row, the number of leading spaces is n - i, which shifts the stars toward the center and produces the diamond shape.

Comment