Print the Diamond Shape

Last Updated : 11 Mar, 2026

Given a number n, print a diamond-shaped star pattern with 2n rows, where the number of stars first increases and then decreases to form the diamond.

Examples : 

Input: 5

Output:

frame_3345
Try It Yourself
redirect icon

Using Nested Loops - O(n²) Time and O(1) Space

The diamond pattern can be generated by printing two triangular patterns. The first part prints the upper half where the number of stars increases in each row while the spaces decrease. The second part prints the lower half where the number of stars decreases and the spaces increase. Nested loops are used to print spaces and stars in each row.

Step-by-Step Approach:

  • Initialize a variable space = n - 1 to track the number of spaces before stars.
  • Run a loop from i = 0 to n - 1 to print the upper half of the diamond.
  • Print space number of spaces using a loop.
  • Print i + 1 stars separated by spaces.
  • Decrease space after each row.
  • Reset space = 0 for the lower half of the diamond.
  • Run another loop from i = n down to 1.
  • Print space number of spaces.
  • Print i stars separated by spaces.
  • Increase space after each row.
C++
//Driver Code Starts
using namespace std;

// Prints diamond pattern with 2n rows 
//Driver Code Ends

void printDiamond(int n) 
{ 
    int space = n - 1; 

    // run loop (parent loop) 
    // till number of rows 
    for (int i = 0; i < n; i++) 
    { 
        // loop for initially space, 
        // before star printing 
        for (int j = 0;j < space; j++) 
            cout << " "; 

        // Print i+1 stars 
        for (int j = 0; j <= i; j++) 
            cout << "* "; 

        cout << endl; 
        space--; 
    } 

    // Repeat again in reverse order 
    space = 0; 

    // run loop (parent loop) 
    // till number of rows 
    for (int i = n; i > 0; i--) 
    { 
        // loop for initially space, 
        // before star printing 
        for (int j = 0; j < space; j++) 
            cout << " "; 

        // Print i stars 
        for (int j = 0;j < i;j++) 
            cout << "* ";

        cout << endl;
        space++; 
    } 
} 


//Driver Code Starts
int main() 
{ 
    printDiamond(5); 
    return 0; 
} 
//Driver Code Ends
C
//Driver Code Starts
#include<stdio.h>

// Prints diamond 
// pattern with 2n rows
//Driver Code Ends

void printDiamond(int n)
{
    int space = n - 1;

    // run loop (parent loop)
    // till number of rows
    for (int i = 0; i < n; i++)
    {
        // loop for initially space, 
        // before star printing
        for (int j = 0;j < space; j++)
            printf(" ");

        // Print i+1 stars
        for (int j = 0;j <= i; j++)
            printf("* ");

        printf("
");
        space--;
    }

    // Repeat again in 
    // reverse order
    space = 0;

    // run loop (parent loop)
    // till number of rows
    for (int i = n; i > 0; i--)
    {
        // loop for initially space, 
        // before star printing
        for (int j = 0; j < space; j++)
            printf(" ");

        // Print i stars
        for (int j = 0;j < i;j++)
            printf("* ");

        printf("
");
        space++;
    }
}

//Driver Code Starts

int main()
{
    printDiamond(5);
    return 0;
}
//Driver Code Ends
Java
//Driver Code Starts
class GFG {

    // Prints diamond pattern
    // with 2n rows
//Driver Code Ends

    static void printDiamond(int n)
    {
        int space = n - 1;

        // run loop (parent loop)
        // till number of rows
        for (int i = 0; i < n; i++) {
            // loop for initially space,
            // before star printing
            for (int j = 0; j < space; j++)
                System.out.print(" ");

            // Print i+1 stars
            for (int j = 0; j <= i; j++)
                System.out.print("* ");

            System.out.print("
");
            space--;
        }

        // Repeat again in
        // reverse order
        space = 0;

        // run loop (parent loop)
        // till number of rows
        for (int i = n; i > 0; i--) {
            // loop for initially space,
            // before star printing
            for (int j = 0; j < space; j++)
                System.out.print(" ");

            // Print i stars
            for (int j = 0; j < i; j++)
                System.out.print("* ");

            System.out.print("
");
            space++;
        }
    }


//Driver Code Starts
    public static void main(String[] args)
    {
        printDiamond(5);
    }
}
//Driver Code Ends
Python
# Prints diamond pattern with 2n rows
def printdiamond(n):

    space = n - 1

    # run loop (parent loop)
    # till number of rows
    for i in range(0, n):

        # loop for initially space,
        # before star printing
        for j in range(0, space):
            print(" ", end="")

        # Print i+1 stars
        for j in range(0, i + 1):
            print("* ", end="")

        print()
        space -= 1

    # Repeat again in reverse order
    space = 0

    # run loop (parent loop)
    # till number of rows
    for i in range(n, 0, -1):

        # loop for initially space,
        # before star printing
        for j in range(0, space):
            print(" ", end="")

        # Print i stars
        for j in range(0, i):
            print("* ", end="")

        print()
        space += 1



#Driver Code Starts
def main():
    printdiamond(5)


if __name__ == "__main__":
    main()

#Driver Code Ends
C#
//Driver Code Starts
using System;

class GFG 
{
    
    // Prints diamond pattern
    // with 2n rows
//Driver Code Ends

    static void printDiamond(int n)
    {
        int space = n - 1;
    
        // run loop (parent loop) 
        // till number of rows
        for (int i = 0; i < n; i++)
        {
            // loop for initially space,
            // before star printing
            for (int j = 0; j < space; j++)
                Console.Write(" ");
    
            // Print i+1 stars
            for (int j = 0; j <= i; j++)
                Console.Write("* ");
    
            Console.Write("
");
            space--;
        }
    
        // Repeat again in
        // reverse order
        space = 0;
    
        // run loop (parent loop)
        // till number of rows
        for (int i = n; i > 0; i--)
        {
            // loop for initially space, 
            // before star printing
            for (int j = 0; j < space; j++)
                Console.Write(" ");
    
            // Print i stars
            for (int j = 0; j < i; j++)
                Console.Write("* ");
    
            Console.Write("
");
            space++;
        }
    }
    

//Driver Code Starts
    public static void Main() 
    {
        printDiamond(5);
        
    }
}
//Driver Code Ends
JavaScript
//Driver Code Starts
// Prints diamond pattern with 2n rows
//Driver Code Ends

function printdiamond(n)
{
    let space = n - 1;

    // run loop (parent loop)
    // till number of rows
    for (let i = 0; i < n; i++)
    {

        // loop for initially space,
        // before star printing
        for (let j = 0; j < space; j++)
            process.stdout.write(" ");

        // Print i+1 stars
        for (let j = 0; j <= i; j++)
            process.stdout.write("* ");

        console.log();
        space--;
    }

    // Repeat again in reverse order
    space = 0;

    // run loop (parent loop)
    // till number of rows
    for (let i = n; i > 0; i--)
    {

        // loop for initially space,
        // before star printing
        for (let j = 0; j < space; j++)
            process.stdout.write(" ");

        // Print i stars
        for (let j = 0; j < i; j++)
            process.stdout.write("* ");

        console.log();
        space++;
    }
}

//Driver Code Starts

// driver code
printdiamond(5);
//Driver Code Ends

Output
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    * 

Using Recursion - O(N²) Time and O(N) Space

In this approach, recursion is used to print spaces and stars for each row of the diamond. Separate recursive functions print blank spaces, stars, the upper half, and the lower half of the diamond.

C++
//Driver Code Starts
using namespace std;

//Driver Code Ends

// Function to print stars in a line of the diamond
void gotonextLine(int k, int i, int z)
{

    // base case
    if (k == i)
        return;
    cout << "* ";
    gotonextLine(k + z, i, z);
}
void addblankSpaceInDiamond(

    // print blank space of diamond
    int j, int i, int z)
{
    if (j == i)
        return;
    cout << " ";
    addblankSpaceInDiamond(j + z, i, z);
}
void upperDiamond(int row, int i)
{
    // base case
    if (i > row)
        return;
    addblankSpaceInDiamond(row, i, -1);
    gotonextLine(0, i, 1);
    cout << endl;

    // recursive call
    upperDiamond(row, i + 1);
}
void lowerDiamond(int row, int i)
{
    // base case
    if (i > row)
        return;
    addblankSpaceInDiamond(0, i, 1);
    gotonextLine(row, i, -1);
    cout << endl;
    lowerDiamond(row, i + 1);
}

//Driver Code Starts
int main()
{
    int row;
    row = 5;

    // print upper part of triangle
    upperDiamond(row, 0);

    // print lower part of diamond
    lowerDiamond(row, 0);
    return 0;
}
//Driver Code Ends
Java
//Driver Code Starts
class GFG {
//Driver Code Ends

    
    // Function to print stars in a line of the diamond
    static void gotonextLine(int k, int i, int z)
    {
        // base case
        if (k == i)
            return;
        System.out.print("* ");
        gotonextLine(k + z, i, z);
    }

    // print blank space of diamond
    static void addblankSpaceInDiamond(int j, int i, int z)
    {
        if (j == i)
            return;
        System.out.print(" ");
        addblankSpaceInDiamond(j + z, i, z);
    }

    static void upperDiamond(int row, int i)
    {
        // base case
        if (i > row)
            return;
        addblankSpaceInDiamond(row, i, -1);
        gotonextLine(0, i, 1);
        System.out.println();

        // recursive call
        upperDiamond(row, i + 1);
    }

    static void lowerDiamond(int row, int i)
    {
        // base case
        if (i > row)
            return;
        addblankSpaceInDiamond(0, i, 1);
        gotonextLine(row, i, -1);
        System.out.println();
        lowerDiamond(row, i + 1);
    }


//Driver Code Starts
    public static void main(String[] args)
    {
        int row;
        row = 5;

        // print upper part of triangle
        upperDiamond(row, 0);

        // print lower part of diamond
        lowerDiamond(row, 0);
    }
}
//Driver Code Ends
Python
# Function to print stars in a line of the diamond
def gotonextline(k, i, z):
    # base case
    if k == i:
        return
    print("*", end=" ")
    gotonextline(k + z, i, z)

# print blank space of diamond
def addblankspaceindiamond(j, i, z):  
    if j == i:
        return
    print(" ", end="")
    addblankspaceindiamond(j + z, i, z)

def upperdiamond(row, i):
    # base case
    if i > row:
        return
    addblankspaceindiamond(row, i, -1)
    gotonextline(0, i, 1)
    print()

    # recursive call
    upperdiamond(row, i + 1)

def lowerdiamond(row, i):
    # base case
    if i > row:
        return
    addblankspaceindiamond(0, i, 1)
    gotonextline(row, i, -1)
    print()
    lowerdiamond(row, i + 1)


def main():
#Driver Code Starts
    row = 5

    # print upper part of triangle
    upperdiamond(row, 0)

    # print lower part of diamond
    lowerdiamond(row, 0)

if __name__ == "__main__":
    main()
#Driver Code Ends
C#
//Driver Code Starts
using System;
//Driver Code Ends


class GFG {
    
    // Function to print stars in a line of the diamond
    static void gotonextLine(int k, int i, int z)
    {
        // base case
        if (k == i)
            return;
        Console.Write("* ");
        gotonextLine(k + z, i, z);
    }

    // print blank space of diamond
    static void addblankSpaceInDiamond(int j, int i, int z)
    {
        if (j == i)
            return;
        Console.Write(" ");
        addblankSpaceInDiamond(j + z, i, z);
    }

    static void upperDiamond(int row, int i)
    {
        // base case
        if (i > row)
            return;
        addblankSpaceInDiamond(row, i, -1);
        gotonextLine(0, i, 1);
        Console.WriteLine();

        // recursive call
        upperDiamond(row, i + 1);
    }

    static void lowerDiamond(int row, int i)
    {
        // base case
        if (i > row)
            return;
        addblankSpaceInDiamond(0, i, 1);
        gotonextLine(row, i, -1);
        Console.WriteLine();
        lowerDiamond(row, i + 1);
    }


//Driver Code Starts
    static void Main()
    {
        int row = 5;

        // print upper part of triangle
        upperDiamond(row, 0);

        // print lower part of diamond
        lowerDiamond(row, 0);
    }
}
//Driver Code Ends
JavaScript
//Driver Code Starts
"use strict";

// Function to print stars in a line of the diamond
//Driver Code Ends

function gotonextLine(k, i, z) {
    // base case
    if (k === i)
        return;
    process.stdout.write("* ");
    gotonextLine(k + z, i, z);
}

// Function to print blank spaces
function addblankSpaceInDiamond(j, i, z) { 
    if (j === i)
        return;
    process.stdout.write(" ");
    addblankSpaceInDiamond(j + z, i, z);
}

// Function to print upper half of diamond
function upperDiamond(row, i) {
    // base case
    if (i > row)
        return;
    addblankSpaceInDiamond(row, i, -1);
    gotonextLine(0, i, 1);
    console.log();

    // recursive call
    upperDiamond(row, i + 1);
}

// Function to print lower half of diamond
function lowerDiamond(row, i) {
    // base case
    if (i > row)
        return;
    addblankSpaceInDiamond(0, i, 1);
    gotonextLine(row, i, -1);
    console.log();
    lowerDiamond(row, i + 1);
}

//Driver Code Starts

// driver code
const row = 5;

// print upper part of triangle
upperDiamond(row, 0);

// print lower part of diamond
lowerDiamond(row, 0);
//Driver Code Ends

Output
     
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    * 
     
Comment