Diamond Pattern in C++

Diamond pattern is nothing but to print in diamond shape. We can print stars, numbers, alphabets in diamond shape. If we enter number of rows then it will print the diamond shape in that rows. We have three coding to print in diamond pattern in this article.

  • Print Diamond Pattern of Stars
  • Print Diamond pattern of Numbers
  • Diamond Pattern of Alphabet Characters
Diamond Pattern in C++

Diamond Pattern in C++

Program Code for Draw Diamond Pattern of Stars in C++

#include<iostream>
using namespace std;
int main()
{
    int i, j, rowNum, space;
    cout<<"Enter the Number of Rows: ";
    cin>>rowNum;
    space = rowNum-1;
    for(i=1; i<=rowNum; i++)
    {
        for(j=1; j<=space; j++)
            cout<<" ";
        space--;
        for(j=1; j<=(2*i-1); j++)
            cout<<"*";
        cout<<endl;
    }
    space = 1;
    for(i=1; i<=(rowNum-1); i++)
    {
        for(j=1; j<=space; j++)
            cout<<" ";
        space++;
        for(j=1; j<=(2*(rowNum-i)-1); j++)
            cout<<"*";
        cout<<endl;
    }
    cout<<endl;
    return 0;
}
Read Also : Factorial Program in C++

Output


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

Diamond Pattern of Numbers in C++

If the user enter number of rows then it will print numbers in diamond shape. If we enter 5 rows then the center row has 5 numbers from 1 to 5. If we enter 10 rows then the center row has 10 numbers from 1 to 10.

#include<iostream>
using namespace std;
int main()
{
    int i, j, rowNum, space, num=1;
    cout<<"Enter the Number of Rows: ";
    cin>>rowNum;
    space = rowNum-1;
    for(i=1; i<=rowNum; i++)
    {
        for(j=1; j<=space; j++)
            cout<<" ";
        space--;
        for(j=1; j<=(2*i-1); j++)
        {
            cout<<num;
            num++;
        }
        cout<<endl;
        num = 1;
    }
    space = 1;
    for(i=1; i<=(rowNum-1); i++)
    {
        for(j=1; j<=space; j++)
            cout<<" ";
        space++;
        for(j=1; j<=(2*(rowNum-i)-1); j++)
        {
            cout<<num;
            num++;
        }
        cout<<endl;
        num = 1;
    }
    cout<<endl;
    return 0;
}

Output

     1
  1  2 3
1 2 3 4 5
   1 2 3
      1  

Diamond Pattern of Alphabets in C++

We can print diamond shape of letters in C++.

#include<iostream>
using namespace std;
int main()
{
    int i, j, rowNum, space;
    char ch='A';
    cout<<"Enter the Number of Rows: ";
    cin>>rowNum;
    space = rowNum-1;
    for(i=1; i<=rowNum; i++)
    {
        for(j=1; j<=space; j++)
            cout<<" ";
        space--;
        for(j=1; j<=(2*i-1); j++)
        {
            cout<<ch;
            ch++;
        }
        cout<<endl;
        ch = 'A';
    }
    space = 1;
    for(i=1; i<=(rowNum-1); i++)
    {
        for(j=1; j<=space; j++)
            cout<<" ";
        space++;
        for(j=1; j<=(2*(rowNum-i)-1); j++)
        {
            cout<<ch;
            ch++;
        }
        cout<<endl;
        ch = 'A';
    }
    cout<<endl;
    return 0;
}

Output

      A
  A  B C
A B C D E
   A B C
      A  

Final Words

I hope this article helps you to Print Diamond Pattern in C++ Program. If you face any issues please let me know via the comment section. Share this article with other C++ program developers via social networks.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment