Diamond pattern program in C++

In this section you will learn how to print diamond pattern in c++ in easy way. We will print the diamond pattern with help of for loop condition and then we will show the output on screen. In this program we are taking the input from user.

How program will work?

First part of loop printing below pattern:


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

Second part of loop printing below pattern:


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

Program of print diamond pattern in c++


	#include<iostream.h>
	#include<conio.h>

	//DIAMOND PATTERN PROGRAM
	void main()
	{
	   int value, i, j, k;
	   clrscr();

	   cout<<"PROGRAM FOR DISPLAYING DIAMOND PATTERN OF *.";
	   cout<<"\nENTER NUMBER TO PRINT THE DIAMOND:- ";
	   cin>>value;

	   //FIRST PART OF LOOP
	   for(i=1; i<=value; i++)
	   {
		for(j=0; j<=(value-i); j++)
		{
		    cout<<" ";
		}

		for(j=1; j<=i; j++)
		{
		    cout<<"*";
		}
		for(k=1; k<i; k++)
		{
		   cout<<"*";
		}
		cout<<"\n";
	   }

	   //SECOND PART OF LOOP
	   for(i=value-1; i>=1; i--)
	   {
		for(j=0; j<=(value-i); j++)
		{
		    cout<<" ";
		}

		for(j=1; j<=i; j++)
		{
		    cout<<"*";
		}
		for(k=1; k<i; k++)
		{
		   cout<<"*";
		}
		cout<<"\n";
	   }

	   getch();
	 }

Output:


	PROGRAM FOR DISPLAYING DIAMOND PATTERN OF *.
	ENTER NUMBER TO PRINT THE DIAMOND:- 5
	
            *
          * * *
        * * * * *
      * * * * * * *
    * * * * * * * * *
      * * * * * * *
        * * * * *
          * * *
            *

Post Your Comment