Program to print Inverse Diamond pattern on C++


In this tutorial, we will be discussing a program to print given inverse diamond pattern.

For this, we will be provided with the value of N. Our task is to print an inverse the diamond pattern according to the height of 2N-1.

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std;
//printing the inverse diamond pattern
void printDiamond(int n){
   cout<<endl;
   int i, j = 0;
   //loop for the upper half
   for (i = 0; i < n; i++) {
      //left triangle
      for (j = i; j < n; j++)
         cout<<"*";
      //middle triangle
      for (j = 0; j < 2 * i + 1; j++)
         cout<<" ";
      //right triangle
      for (j = i; j < n; j++)
         cout<<"*";
      cout<<endl;
   }
   //loop for the lower half
   for (i = 0; i < n - 1; i++) {
      //left triangle
      for (j = 0; j < i + 2; j++)
         cout<<"*";
      //middle triangle
      for (j = 0; j < 2 * (n - 1 - i) - 1; j++)
         cout<<" ";
      //right triangle
      for (j = 0; j < i + 2; j++)
         cout<<"*";
      cout<<endl;
   }
   cout<<endl;
}
int main(){
   int n = 5;
   printDiamond(n);
   return 0;
}

Output

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

Updated on: 02-Jan-2020

260 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements