C++ Program to Print Diamond Pattern

In this article, you will learn and get code to print diamond pattern in C++ language. Here are the list of programs on printing of diamond pattern, available in this article:

  • Print Diamond Pattern of Stars
  • of Numbers
  • of Alphabet Characters

Print Diamond Pattern of Stars

To print diamond pattern of stars in C++ programming, you have to ask from user to enter the number of rows. Now using the row size, print diamond pattern as shown in the program given below:

If user enters 6 as row size of diamond, then its upper-triangular part expands upto 6 lines, whereas its lower-triangular part expands upto 5 (one less than row size) lines.

The question is, write a program in C++ to print diamond pattern of stars. Here is its answer:

#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;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program print diamond pattern

Now supply the input say 6 as number of rows, and press ENTER key to print diamond pattern as shown in the snapshot given below:

print diamond pattern c++

Note - Here the input 6 is not the actual size of row, it is row size of upper triangular part of diamond pattern.

There are three for loops used to create both upper and lower triangle of diamond. The first three for loops is created in a way that:

  • First corresponds to rows
  • Second corresponds to spaces
  • Third corresponds to columns (printing of stars)

And the three for loop for lower triangular part of diamond pattern also works in a similar way.

The dry run of following block of code:

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;
}

that prints upper triangular part of diamond, goes like:

  • When user enters row size as 6, then it gets stored in rowNum
  • And rowNum-1 or 6-1 or 5 gets stored in space
  • Now program flow starts the execution of for loop. That is, i=0 and the condition i<=rowNum or 0<=6 evaluates to be true
  • Therefore program flow goes inside the loop, now j=1 and the condition j<=space or 1<=5 evaluates to be true
  • Therefore program flow goes inside this loop and prints a single space
  • Now the value of j gets incremented. So j=2 and the condition j<=space or 2<=space or 2<=5 evaluates to be true again, therefore another spaces gets printed
  • In this way, there are 4 spaces gets printed, then the condition evaluated to be false
  • Every time after exiting from this loop, decrement the value of space to print one less space from next line or row
  • Using the third for loop, print stars
  • That is, at first execution, j=1 and the condition j<=(2*i-1) or j<=(2*1-1) or 1<=(1) or 1<=1 evaluates to be true, therefore program flow goes inside this loop and prints a star (*)
  • Now the value of j gets incremented. So j=2
  • And the condition j<=(2*i-1) or 2<=(2*1-1) or 2<=(1) evaluates to be false, therefore program ends this loop
  • Using endl, next thing start from new line
  • Now program flow increments the value of outer for loop's variable, that is i. So i=2
  • And the condition i<=rowNum or 2<=6 evaluates to be true again, therefore program flow goes inside the loop again, this process continues until the condition evaluates to be false

In this way, the star pattern of upper diamond gets printed, in similar way, lower diamond also gets printed.

Print Diamond Pattern of Numbers

This is the same program as of previous one. The only difference is, instead of stars, we've used numbers to print diamond pattern of numbers.

#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;
}

Here is its sample run with user input, 5:

print diamond pattern of numbers c++

Print Diamond Pattern of Alphabet Characters

This is the last program that creates diamond pattern of alphabet characters.

#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;
}

Here is its sample run with user input, 5 as size of rows:

print diamond pattern of alphabets c++

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »



Like/Share Us on Facebook 😋