codescracker
c++

C++ Program to Print Diamond Pattern



« Previous Program Next Program »


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

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:

// C++ Program to Print Diamond Pattern of Stars
// ----codescracker.com----

#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:

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 triangul part of diamond, goes like:

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.

// C++ Program to Print Diamond Pattern of Stars
// ----codescracker.com----

#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.

// C++ Program to Print Diamond Pattern of Stars
// ----codescracker.com----

#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


« Previous Program Next Program »