Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Program to print Inverse Diamond pattern

  • Difficulty Level : Easy
  • Last Updated : 21 Apr, 2021

Given an integer n, the task is to print the Inverse Diamond Pattern in 2n-1 rows.
Example: 
 

Input: n = 3
Output:

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

Input: n = 7
Output:

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

 

Hey! Looking for some great resources suitable for young ones? You've come to the right place. Check out our self-paced courses designed for students of grades I-XII

Start with topics like Python, HTML, ML, and learn to make some games and apps all with the help of our expertly designed content! So students worry no more, because GeeksforGeeks School is now here!

 



Approach: 
 

  1. The full Inverse Diamond is of 2n-1 rows for an input of n.
  2. The program is divided to print this pattern in two parts: 
    • The first part is the top half of diamond of n rows.
    • This part includes 3 parts- the left triangle of *, the middle triangle of space and the right triangle of *.
    • The second part is the below half of diamond of n-1 rows.
    • This part also includes 3 parts- the left triangle of *, the middle triangle of space and the right triangle of *.
  3. Printing each part, the required Inverse Diamond Pattern is obtained.

Below is the implementation of the above approach: 
 

C++




// C++ Code to print
// the inverse diamond shape
   
#include<bits/stdc++.h>
 using namespace std;
   
    // Function to Print Inverse Diamond pattern
    // with 2n-1 rows
void printDiamond(int n)
    {
        cout<<endl;
   
        int i, j = 0;
   
        // for top half
        for (i = 0; i < n; i++) {
   
            // for left *
            for (j = i; j < n; j++)
                cout<<"*";
   
            // for middle " "
            for (j = 0; j < 2 * i + 1; j++)
                cout<<" ";
   
            // for right *
            for (j = i; j < n; j++)
                cout<<"*";
   
            cout<<endl;
        }
   
        // for below half
        for (i = 0; i < n - 1; i++) {
   
            // for left *
            for (j = 0; j < i + 2; j++)
                cout<<"*";
   
            // for middle " "
            for (j = 0; j < 2 * (n - 1 - i) - 1; j++)
                cout<<" ";
   
            // for right *
            for (j = 0; j < i + 2; j++)
                cout<<"*";
   
            cout<<endl;
        }
   
        cout<<endl;
    }
   
    // Driver Code
    int main()
    {
   
        // Define n
        int n = 3;
        cout<<"Inverse Diamond Pattern for n = "<<n;
        printDiamond(n);
   
        n = 7;
        cout<<"\nInverse Diamond Pattern for n = "<<n;
        printDiamond(n);
    }

Java




// Java Code to print
// the inverse diamond shape
 
import java.util.*;
 
class GFG {
 
    // Function to Print Inverse Diamond pattern
    // with 2n-1 rows
    static void printDiamond(int n)
    {
        System.out.println();
 
        int i, j = 0;
 
        // for top half
        for (i = 0; i < n; i++) {
 
            // for left *
            for (j = i; j < n; j++)
                System.out.print("*");
 
            // for middle " "
            for (j = 0; j < 2 * i + 1; j++)
                System.out.print(" ");
 
            // for right *
            for (j = i; j < n; j++)
                System.out.print("*");
 
            System.out.println();
        }
 
        // for below half
        for (i = 0; i < n - 1; i++) {
 
            // for left *
            for (j = 0; j < i + 2; j++)
                System.out.print("*");
 
            // for middle " "
            for (j = 0; j < 2 * (n - 1 - i) - 1; j++)
                System.out.print(" ");
 
            // for right *
            for (j = 0; j < i + 2; j++)
                System.out.print("*");
 
            System.out.println();
        }
 
        System.out.println();
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Define n
        int n = 3;
        System.out.println("Inverse Diamond Pattern for n = " + n);
        printDiamond(n);
 
        n = 7;
        System.out.println("\nInverse Diamond Pattern for n = " + n);
        printDiamond(n);
    }
}

Python3




#Python3 program to print
# the inverse diamond shape
     
 
# Function to Print Inverse
# Diamond pattern
# with 2n-1 rows
def printDiamond(n) :
    print("")
    j = 0
     
    # for top half
    for i in range(0,n):
         
        # for left *
        for j in range(i,n):
            print("*",end="")
             
        # for middle " "
        for j in range(0,2 * i + 1):
            print(" ",end="")
             
        # for right *
        for j in range(i,n):
            print("*",end="")
             
        print("")
     
    # for below half
    for i in range(0,n-1):
         
        # for left *
        for j in range(0, i+2):
            print("*",end="")
             
        # for middle " "
        for j in range(0,2 * (n - 1 - i) - 1):
            print(" ",end="")
             
        # for right *
        for j in range(0, i+2):
            print("*",end="")
             
        print("")
         
    print("")
     
     
# Driver Code
 
if __name__=='__main__':
     
# Define n
    n = 3
    print("Inverse Diamond Pattern for n = ",n)
    printDiamond(n)
     
    n = 7
    print("\nInverse Diamond Pattern for n = ",n )
    printDiamond(n)
 
# this code is contributed by Smitha Dinesh Semwal

C#




// C# Code to print
// the inverse diamond shape
using System;
 
class GFG
{
 
// Function to Print Inverse
// Diamond pattern with 2n-1 rows
static void printDiamond(int n)
{
    Console.WriteLine();
 
    int i, j = 0;
 
    // for top half
    for (i = 0; i < n; i++)
    {
 
        // for left *
        for (j = i; j < n; j++)
            Console.Write("*");
 
        // for middle " "
        for (j = 0; j < 2 * i + 1; j++)
            Console.Write(" ");
 
        // for right *
        for (j = i; j < n; j++)
            Console.Write("*");
 
        Console.WriteLine();
    }
 
    // for below half
    for (i = 0; i < n - 1; i++)
    {
 
        // for left *
        for (j = 0; j < i + 2; j++)
            Console.Write("*");
 
        // for middle " "
        for (j = 0; j < 2 * (n - 1 - i) - 1; j++)
            Console.Write(" ");
 
        // for right *
        for (j = 0; j < i + 2; j++)
            Console.Write("*");
 
        Console.WriteLine();
    }
 
    Console.WriteLine();
}
 
// Driver Code
public static void Main()
{
 
    // Define n
    int n = 3;
    Console.WriteLine("Inverse Diamond " +
                      "Pattern for n = " + n);
    printDiamond(n);
 
    n = 7;
    Console.WriteLine("\nInverse Diamond " +
                        "Pattern for n = " + n);
    printDiamond(n);
}
}
 
// This code is contributed
// by inder_verma.

PHP




<?php
// PHP Code to print
// the inverse diamond shape
 
// Function to Print Inverse
// Diamond pattern with 2n-1 rows
function printDiamond($n)
{
    echo"\n";
 
    $i; $j = 0;
 
    // for top half
    for ($i = 0; $i < $n; $i++)
    {
 
        // for left *
        for ($j = $i; $j < $n; $j++)
            echo("*");
 
        // for middle " "
        for ($j = 0;
             $j < 2 * $i + 1; $j++)
            echo(" ");
 
        // for right *
        for ($j = $i; $j < $n; $j++)
            echo("*");
 
        echo("\n");
    }
 
    // for below half
    for ($i = 0; $i < $n - 1; $i++)
    {
 
        // for left *
        for ($j = 0; $j < $i + 2; $j++)
            echo("*");
 
        // for middle " "
        for ($j = 0;
             $j < 2 * ($n - 1 - $i) - 1; $j++)
            echo(" ");
 
        // for right *
        for ($j = 0; $j < $i + 2; $j++)
            echo("*");
 
        echo("\n");
    }
 
    echo("\n");
}
 
// Driver Code
 
// Define n
$n = 3;
echo("Inverse Diamond Pattern for n = " );
echo($n);
printDiamond($n);
 
$n = 7;
echo("\nInverse Diamond Pattern for n = " );
echo($n);
printDiamond($n);
 
// This code is contributed
// by inder_verma.
?>

Javascript




<script>
 
      // JavaScript Code to print
      // the inverse diamond shape
 
      // Function to Print Inverse Diamond pattern
      // with 2n-1 rows
      function printDiamond(n) {
        document.write("<br><br>");
 
        var i,
          j = 0;
 
        // for top half
        for (i = 0; i < n; i++) {
          // for left *
          for (j = i; j < n; j++)
          document.write("*");
 
          // for middle " "
          for (j = 0; j < 2 * i + 1; j++)
          document.write("  ");
 
          // for right *
          for (j = i; j < n; j++)
          document.write("*");
 
          document.write("<br>");
        }
 
        // for below half
        for (i = 0; i < n - 1; i++) {
          // for left *
          for (j = 0; j < i + 2; j++)
          document.write("*");
 
          // for middle " "
          for (j = 0; j < 2 * (n - 1 - i) - 1; j++)
            document.write("  ");
 
          // for right *
          for (j = 0; j < i + 2; j++)
          document.write("*");
 
          document.write("<br>");
        }
 
        document.write("<br>");
      }
 
      // Driver Code
      // Define n
      var n = 3;
      document.write("Inverse Diamond Pattern for n = " + n);
      printDiamond(n);
 
      n = 7;
      document.write("\nInverse Diamond Pattern for n = " + n);
      printDiamond(n);
       
</script>
Output: 
Inverse Diamond Pattern for n = 3

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


Inverse Diamond Pattern for n = 7

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

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!