C++ program to print a diamond shape star pattern

C++ program to print a diamond shape:

In this C++ program, we will learn how to print a diamond shape with user given height for the shape. I will explain you the algorithm before start writing the program.

Algorithm to print a diamond shape:

A diamond shape pattern of height 6 looks as below:

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

If you look closely, we are using blank spaces and * to print this pattern. If I replace the blank spaces with #, it will look as below:

#####*
####***
###*****
##*******
#*********
***********
#*********
##*******
###*****
####***
#####*

So, for a diamond shape pattern of height 6,

  • The first line prints 5 spaces and one *.
  • The second line prints 4 spaces and three * etc.
  • It reaches the middle of the pattern after 6 lines are printed.
  • After middle, it prints the patterns in opposite manner. For the first line it prints one space, for the second line it prints two spaces etc.

We can use two loops to print the pattern.

  • Ask the user to enter the height of the pattern and save it in a variable height.
  • Initialize one variable space as height - 1 to denote the number of spaces to print in each row.
  • Run one loop from i = 1 to i = height. On each iteration, first print blank spaces for space number of times and print stars for 2*i - 1 number of times. At the end of each iteration of the loop, decrement the value of space by 1 and print a new line.
  • Once the first loop ends, it will print the pattern up to the height row.
  • Assign 1 to the space variable and run one loop from i = 1 to i = height - 1. On each iteration, print blank spaces for space number of times and print stars for 2*(height - i) - 1 number of times. At the end of each iteration, increase the value of the space variable by 1 and print one newline.

Let’s use the above algorithm to write down the complete program.

C++ program to print a diamond shape:

The below C++ program prints a diamond shape:

#include <iostream>
using namespace std;

int main()
{
    int i, j, height;
    cout << "Enter the height of the pattern: ";
    cin >> height;

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

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

        cout << endl;
        space--;
    }

    space = 1;

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

        for (j = 1; j <= (2 * (height - i) - 1); j++)
            cout << "*";

        cout << endl;
        space++;
    }

}
  • This program uses two for loops as explained above.
  • The first for loop prints the first half of the pattern and the second for loop prints the second half of the pattern.

If you run this program, it will print output as below:

Enter the height of the pattern: 7
      *
     ***
    *****
   *******
  *********
 ***********
*************
 ***********
  *********
   *******
    *****
     ***
      *

C++ print diamond pattern

C++ program to print a diamond shape with while loops:

Let’s use while loops to print the diamond shape pattern.

#include <iostream>
using namespace std;

int main()
{
    int i, j, height;
    cout << "Enter the height of the pattern: ";
    cin >> height;

    int space = height - 1;

    i = 1;
    while (i <= height)
    {
        j = 1;
        while (j <= space)
        {
            cout << " ";
            j++;
        }

        j = 1;
        while (j <= (2 * i - 1))
        {
            cout << "*";
            j++;
        }

        cout << endl;
        i++;
        space--;
    }

    space = 1;
    i = 1;
    while (i <= (height - 1))
    {
        j = 1;
        while (j <= space)
        {
            cout << " ";
            j++;
        }

        j = 1;
        while (j <= (2 * (height - i) - 1))
        {
            cout << "*";
            j++;
        }

        i++;
        cout << endl;
        space++;
    }
}

It will print similar result.

Enter the height of the pattern: 8
       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************
 *************
  ***********
   *********
    *******
     *****
      ***
       *

You might also like: