# Diamond pattern problems in C++

## Part-5: Diamond pattern problems

In this article, we are going to discuss about some of the various diamond pattern problems using stars.

### Topics Covered

1. Print diamond using stars
2. Hollow diamond using stars
3. Hollow diamond inscribed in a rectangle

## 1. Diamond using stars

``````    *
***
*****
*******
*********
*********
*******
*****
***
*``````

Look at the pattern, you realize that the diamond is nothing but a combination of two pyramids. This is like you put a mirror at the height you given. So, the concept is simple as we do previously in part-2, with an extra loop added for the bottom part. Let’s apply this concept in the code

CODE:

``````#include<iostream>
using namespace std;
int main()
{
int h;
cout<<"Enter the height"<<endl;
cin>>h;

int space=(2*h-1)/2;
for(int i=1; i<=h; i++)
{
for(int j=1; j<=space; j++)
{
cout<<" ";
}
for(int j=1; j<=2*i-1;j++)
{
cout<<"*";
}
cout<<endl;
space--;
}

space = 0;
for(int i=h; i>=1; i--)
{
for(int j=1; j<=space; j++)
{
cout<<" ";
}
for (int j=1; j<=2*i-1; j++)
{
cout<<"*";
}
cout<<endl;
space++;
}
return 0;
}``````

OUTPUT:

## 2. Hollow Diamond using stars

``````    *
* *
*   *
*     *
*       *
*       *
*     *
*   *
* *
*``````

Hollow diamond pattern is same as previous one the only change that we have to do in the code that when we are printing stars in the pyramid, apply a if-else condition that prints stars and spaces according to our requirement. Let’s try this concept with code

CODE:

``````#include<iostream>
using namespace std;
int main()
{
int h;
cout<<"Enter the height"<<endl;
cin>>h;

int space=(2*h-1)/2;
for(int i=1; i<=h; i++)
{
for(int j=1; j<=space; j++)
{
cout<<" ";
}
for(int j=1; j<=2*i-1;j++)
{
if(j==1 || j==2*i-1)
{
cout<<"*";
}
else cout<<" ";
}
cout<<endl;
space--;
}

space = 0;
for(int i=h; i>=1; i--)
{
for(int j=1; j<=space; j++)
{
cout<<" ";
}
for (int j=1; j<=2*i-1; j++)
{
if(j==1 || j==2*i-1)
{
cout<<"*";
}
else cout<<" ";
}
cout<<endl;
space++;
}
return 0;
}``````

OUTPUT:

### 3. Hollow Diamond inscribed in a rectangle using stars

``````*********
**** ****
***   ***
**     **
*       *
*       *
**     **
***   ***
**** ****
*********``````

When we look at the pattern, we found that this pattern looks like the hollow diamond but the outer space is filled with the stars. So, what we have to do is just use the previous code and add a loop in each section that prints the stars in place of spaces outside of the diamond pattern. Let’s try this with the code

CODE:

``````#include<iostream>
using namespace std;
int main()
{
int h;
cout<<"Enter the height"<<endl;
cin>>h;

int space=(2*h-1)/2;
for(int i=1; i<=h; i++)
{
for(int j=1; j<=space; j++)
{
cout<<"*";
}
for(int j=1; j<=2*i-1;j++)
{
if(j==1 || j==2*i-1)
{
cout<<"*";
}
else cout<<" ";
}
for(int j=1; j<=space; j++)
{
cout<<"*";
}
cout<<endl;
space--;
}

space = 0;
for(int i=h; i>=1; i--)
{
for(int j=1; j<=space; j++)
{
cout<<"*";
}
for (int j=1; j<=2*i-1; j++)
{
if(j==1 || j==2*i-1)
{
cout<<"*";
}
else cout<<" ";
}
for(int j=1; j<=space; j++)
{
cout<<"*";
}
cout<<endl;
space++;
}
return 0;
}``````

OUTPUT:

These are some of the problems related to diamond shape that we solve with the help of code. Try many different problems like that by making your own, that helps you in better understanding of the pattern.

In Part-6, we will discuss about some of the advanced pattern problems.

We will be happy to hear your thoughts Hello!👋