Draw Empty Diamond with Plus inside in C++

Draw Empty Diamond with Plus Inside C++

Code:

#include<iostream>
using namespace std;
int main()
{
	int n;
	cout<<"Enter number of rows"<<endl;
	cin>>n;

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

		for(j=n;j>=i;j--)
		{
			cout<<" ";
		}
    	cout<<"*";
		for(k=0;k<(2*i-1);k++)
		{
			if(k==(2*i-1)/2)
			cout<<"*";
			else if(i==n)
			cout<<"*";
			else
			cout<<" ";
		}
      	cout<<"*"<<endl;
	}
	for(i=n-1;i>=1;i--)
	{
		for(j=n;j>=i;j--)
		{
			cout<<" ";
		}
			cout<<"*";

		for(k=0;k<(2*i-1);k++)
		{
			if(k==(2*i-1)/2)
			cout<<"*";
			else
			cout<<" ";

		}
		cout<<"*"<<endl;
	}
	for(i=0;i<=n;i++)
	{
		cout<<" ";
	}
	cout<<"*";

}

Output:

Enter number of rows
7

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

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *