codescracker


c++

C++ Program to Print Diamond Pattern



« Previous Program Next Program »


Print Diamond Pattern in C++

To print diamond pattern of stars in C++ programming, you have to ask from the user to enter the number of rows (upto which he/she want to print the diamond patter) for dimension of diamond. Now to print diamond pattern of stars, use six for loops. The first for loop (outer for loop which contains two for loops. One to print spaces and the second to print stars) is to print pyramid pattern of stars. The second for loop (the second outer for loop which also contains two for loops. One to print spaces and second to print stars) is used to print reverse pyramid of stars. Which wholly makes a diamond pattern of stars as shown here in the following program.

C++ Programming Code to Print Diamond Pattern

Following C++ program ask to the user to enter number of rows for diamond dimension to print the diamond pattern, then display the result on the screen:

/* C++ Program - Print Diamond Pattern */
		
#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    int n, c, k, space=1;
    cout<<"Enter number of rows (for diamond dimension) : ";
    cin>>n;
    space=n-1;
    for (k=1; k<=n; k++)
    {
	for(c=1; c<=space; c++)
	{
		cout<<" ";
	}
	space--;
	for(c=1; c<=(2*k-1); c++)
	{
		cout<<"*";
	}
	cout<<"\n";
    }
    space=1;
    for(k=1; k<=(n-1); k++)
    {
	for(c=1; c<=space; c++)
	{
		cout<<" ";
	}
	space++;
	for(c=1 ; c<=(2*(n-k)-1); c++)
	{
		cout<<" ";
	}
	cout<<"\n";
    }
    getch();
}

When the above C++ program is compile and executed, it will produce the following result:

C++ program print diamond pattern

Same Program in Other Language

You may like the same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test