Open In App

Program to print the diamond shape

Last Updated : 18 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a number n, write a program to print a diamond shape with 2n rows.

Examples : 

diamond

C++




// C++ program to print diamond shape
// with 2n rows 
#include <bits/stdc++.h>
using namespace std;
  
// Prints diamond pattern with 2n rows 
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 
int main() 
    printDiamond(5); 
    return 0; 
  
// This is code is contributed
// by rathbhupendra


C




// C program to print 
// diamond shape with 
// 2n rows
#include<stdio.h>
  
// Prints diamond 
// pattern with 2n rows
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("\n");
        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("\n");
        space++;
    }
}
  
// Driver code
int main()
{
    printDiamond(5);
    return 0;
}


Java




// JAVA Code to print 
// the diamond shape
import java.util.*;
  
class GFG
{
      
    // Prints diamond pattern
    // with 2n rows
    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("\n");
            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("\n");
            space++;
        }
    }
      
    // Driver Code
    public static void main(String[] args) 
    {
        printDiamond(5);
          
    }
}
  
// This code is contributed
// by Arnav Kr. Mandal. 


Python3




# Python program to
# print Diamond shape
  
# Function to print
# Diamond shape
def Diamond(rows):
    n = 1
    for i in range(1, rows + 1):
        # loop to print spaces
        for j in range (1, (rows - i) + 1):
            print(end = " ")
          
        # loop to print star
        while n != (i+1):
            print("*", end = " ")
            n = n + 1
        n = 1
          
        # line break
        print()
  
    k = 0
    n = 0
    for i in range(1, rows + 1):
        # loop to print spaces
        for j in range (1, k + 1):
            print(end = " ")
        k = k + 1
          
        # loop to print star
        while n <= (rows - i):
            print("*", end = " ")
            n = n + 1
        n = 0
        print()
  
# Driver Code
# number of rows input
rows = 5
Diamond(rows)


C#




// C# Code to print 
// the diamond shape
using System;
  
class GFG 
{
      
    // Prints diamond pattern
    // with 2n rows
    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("\n");
            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("\n");
            space++;
        }
    }
      
    // Driver Code
    public static void Main() 
    {
        printDiamond(5);
          
    }
}
  
// This code is contributed 
// by Smitha Semwal. 


PHP




<?php
// PHP program to print 
// diamond shape with 
// 2n rows
  
// Prints diamond $
// pattern with 2n rows
function printDiamond($n)
{
    $space = $n - 1;
  
    // run loop (parent loop)
    // till number of rows
    for ($i = 0; $i < $n; $i++)
    {
          
        // loop for initially space, 
        // before star printing
        for ($j = 0;$j < $space; $j++)
            printf(" ");
  
        // Print i+1 stars
        for ($j = 0;$j <= $i; $j++)
            printf("* ");
  
        printf("\n");
        $space--;
    }
  
    // Repeat again in 
    // reverse order
    $space = 0;
  
    // run loop (parent loop)
    // till number of rows
    for ($i = $n; $i > 0; $i--)
    {
          
        // loop for initially space, 
        // before star printing
        for ($j = 0; $j < $space; $j++)
            printf(" ");
  
        // Pr$i stars
        for ($j = 0;$j < $i;$j++)
            printf("* ");
  
        printf("\n");
        $space++;
    }
}
  
    // Driver code
    printDiamond(5);
  
// This code is contributed by Anuj_67
?>


Javascript




<script>
      // JavaScript program to print diamond shape
      // with 2n rows
  
      // Prints diamond pattern with 2n rows
      function printDiamond(n) {
        var space = n - 1;
  
        // run loop (parent loop)
        // till number of rows
        for (var i = 0; i < n; i++) {
          // loop for initially space,
          // before star printing
          for (var j = 0; j < space; j++) document.write("  ");
  
          // Print i+1 stars
          for (var j = 0; j <= i; j++) document.write("*" + "  ");
  
          document.write("<br>");
          space--;
        }
  
        // Repeat again in reverse order
        space = 0;
  
        // run loop (parent loop)
        // till number of rows
        for (var i = n; i > 0; i--) 
        {
          
          // loop for initially space,
          // before star printing
          for (var j = 0; j < space; j++) document.write("  ");
  
          // Print i stars
          for (var j = 0; j < i; j++) document.write("*" + "  ");
  
          document.write("<br>");
          space++;
        }
      }
  
      // Driver code
      printDiamond(5);
        
      // This code is contributed by rdtank.
    </script>


Output

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

Time Complexity: O(n*n) since we are traversing rows and columns of a grid for printing spaces ‘ ‘ and star ‘*’.
Auxiliary Space: O(1), No extra Space used.

Approach 2: Solving the problem using Recursion

Implementation:

C++




// C++ program to print diamond pattern using recursion
#include <bits/stdc++.h>
using namespace std;
void gotonextLine(int k, int i, int z)
{
    if (k == i) // base case
        return;
    cout << "* ";
    gotonextLine(k + z, i, z);
}
void addblankSpaceInDiamond(
    int j, int i, int z) // print blank space of diamond
{
    if (j == i)
        return;
    cout << " ";
    addblankSpaceInDiamond(j + z, i, z);
}
void upperDiamond(int row, int i)
{
    if (i > row) // base case
        return;
    addblankSpaceInDiamond(row, i, -1);
    gotonextLine(0, i, 1);
    cout << endl;
    upperDiamond(row, i + 1); // recursive call
}
void lowerDiamond(int row,
                  int i) // print the next line of diamond
{
    if (i > row) // base case
        return;
    addblankSpaceInDiamond(0, i, 1);
    gotonextLine(row, i, -1);
    cout << endl;
    lowerDiamond(row, i + 1);
}
int main()
{
    int row;
    row = 5;
    upperDiamond(row, 0); // print upper part of triangle
    lowerDiamond(row, 1); // print lower part of diamond
    return 0;
    // this code is contributed by Shivesh Kumar Dwivedi
}


Java




// Java program to print diamond pattern using recursion
import java.io.*;
   
class GFG{
       
static void gotonextLine(int k, int i, int z)
{
    if (k == i) // base case
        return;
    System.out.print("* ");
    gotonextLine(k + z, i, z);
}
static void addblankSpaceInDiamond(int j, int i, int z) // print blank space of diamond
{
    if (j == i)
        return;
    System.out.print(" ");
    addblankSpaceInDiamond(j + z, i, z);
static void upperDiamond(int row, int i)
{
    if (i > row) // base case
        return;
    addblankSpaceInDiamond(row, i, -1);
    gotonextLine(0, i, 1);
    System.out.print("\n");
    upperDiamond(row, i + 1); // recursive call
}
static void lowerDiamond(int row, int i) // print the next line of diamond
{
    if (i > row) // base case
        return;
    addblankSpaceInDiamond(0, i, 1);
    gotonextLine(row, i, -1);
    System.out.print("\n");
    lowerDiamond(row, i + 1);
}
   
// Driver Code
public static void main(String[] args)
{
    int row;
    row = 5;
    upperDiamond(row, 0); // print upper part of triangle
    lowerDiamond(row, 1); // print lower part of diamond
}
}
   
// This code is contributed by adityapatil12


Python3




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("\n",end=""),
    upperDiamond(row, i + 1) # recursive call
  
  
def lowerDiamond(row,i):
  # print the next line of diamond
    if (i > row): # base case
      return
    addblankSpaceInDiamond(0, i, 1)
    gotonextLine(row, i, -1)
    print("\n",end=""),
    lowerDiamond(row, i + 1)
  
# Code
row = 5
upperDiamond(row, 0) # print upper part of triangle
lowerDiamond(row, 1) # print lower part of diamond
  
# This code is contributed by akashish__


C#




using System;
  
public class GFG{
  
  public static void gotonextLine(int k, int i, int z)
  {
    if (k == i) // base case
      return;
    Console.Write("* ");
    gotonextLine(k + z, i, z);
  }
  
  public static void addblankSpaceInDiamond(
    int j, int i, int z) // print blank space of diamond
  {
    if (j == i)
      return;
    Console.Write(" ");
    addblankSpaceInDiamond(j + z, i, z);
  }
  
  public static void upperDiamond(int row, int i)
  {
    if (i > row) // base case
      return;
    addblankSpaceInDiamond(row, i, -1);
    gotonextLine(0, i, 1);
    Console.Write("\n");
    upperDiamond(row, i + 1); // recursive call
  }
  
  
  public static void lowerDiamond(int row,
                                  int i) // print the next line of diamond
  {
    if (i > row) // base case
      return;
    addblankSpaceInDiamond(0, i, 1);
    gotonextLine(row, i, -1);
    Console.Write("\n");
    lowerDiamond(row, i + 1);
  }
  
  public static void Main ()
  {
  
    // Code
    int row;
    row = 5;
    upperDiamond(row, 0); // print upper part of triangle
    lowerDiamond(row, 1); // print lower part of diamond
  }
}
  
// This code is contributed by akashish__


Javascript




// JavaScript program to print diamond pattern using recursion         
function gotonextLine(k, i, z)
{
    if (k == i) // base case
        return;
    console.log("* ");
    gotonextLine(k + z, i, z);
}
function addblankSpaceInDiamond(j, i, z) // print blank space of diamond
{
    if (j == i)
        return;
    console.log(" ");
    addblankSpaceInDiamond(j + z, i, z);
function upperDiamond(row, i)
{
    if (i > row) // base case
        return;
    addblankSpaceInDiamond(row, i, -1);
    gotonextLine(0, i, 1);
    console.log("<br>");
    upperDiamond(row, i + 1); // recursive call
}
function lowerDiamond(row, i) // print the next line of diamond
{
    if (i > row) // base case
        return;
    addblankSpaceInDiamond(0, i, 1);
    gotonextLine(row, i, -1);
    console.log("<br>");
    lowerDiamond(row, i + 1);
}
   
// Driver Code 
    let row;
    row = 5;
    upperDiamond(row, 0); // print upper part of triangle
    lowerDiamond(row, 1); // print lower part of diamond
  
// This code is contributed by agfro1cac


Output

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

Time Complexity: O(N2), Since we are traversing rows and columns of a grid for printing spaces ‘ ‘ and star ‘*’.
Auxiliary Space: O(N), The extra space is used in recursion call stack.

 



Previous Article
Next Article

Similar Reads

C++ Program To Print The Diamond Shape
Given a number n, write a program to print a diamond shape with 2n-1 rows.Examples : Input: 5Output: C/C++ Code // C++ program to print diamond shape // with 2n-1 rows #include &lt;bits/stdc++.h&gt; using namespace std; // Prints diamond pattern with 2n-1 rows void printDiamond(int n) { int space = n - 1; // run loop (parent loop) // till number of
2 min read
C++ Program To Print Hollow Star Pyramid Diamond Shape Pattern
Here, we will build a C++ program to print the hollow star pyramid diamond shape pattern that can be achieved with two approaches i.e. Using for LoopUsing while loop Input: n = 5 Output: * * * * * * * * * * * * * * * *1. Using for loop C/C++ Code // C++ program to print hollow diamond pattern #include &lt;iostream&gt; using namespace std; int main(
3 min read
Draw Diamond shape using Turtle graphics in Python
In this article, we are going to learn how to draw the shape of a Diamond using turtle graphics in Python. Turtle graphics: forward(length): moves the pen in the forward direction by x unit.right(angle): rotate the pen in the clockwise direction by an angle x.left(angle): rotate the pen in the anticlockwise direction by an angle x. Approach: Import
2 min read
Draw Shape inside Shape in Python Using Turtle
Prerequisites: Turtle Programming in Python Turtle is a Python feature like a drawing board, which let us command a turtle to draw all over it! We can use many turtle functions which can move the turtle around. Turtle comes in the turtle library. The turtle module can be used in both object-oriented and procedure-oriented ways. Some of the commonly
3 min read
Program to print numbers with diamond pattern
write a program where each column represents same number according to given example:Examples : Input : 5 Output : 1 212 32123 212 1 Input : 7 Output : 1 212 32123 4321234 32123 212 1 C/C++ Code // C++ program to print diamond pattern #include&lt;iostream&gt; using namespace std; void display(int n) { // sp stands for space // st stands for number i
6 min read
Program to print Inverse Diamond pattern
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: ******* ******* ****** ****** ***** ***** **** **** *** *** ** ** * * ** ** *** *** **** **** ***** ***** ****** ****** ******* ******* Approach: The full Inverse Diamond is of 2n-1 row
8 min read
Program to print half diamond Number-Star pattern
Given a number N which represents the number of rows. The task is to print a half diamond Number-Star pattern as shown in the below examples.Note: N is always an even number.Examples: Input: N = 4 Output: 2*2 1 1 2*2 Input: N = 6 Output: 3*3*3 2*2 1 1 2*2 3*3*3 On carefully observing the above pattern, it can be broken down into two different trian
6 min read
Python Program to print a number diamond of any given size N in Rangoli Style
Given an integer N, the task is to print a number diamond of size N in rangoli style where N means till Nth number from number ‘1’. Examples: Input : 2 Output : --2-- 2-1-2 --2-- Input : 3 Output : ----3---- --3-2-3-- 3-2-1-2-3 --3-2-3-- ----3---- Input : 4 Output : ------4------ ----4-3-4---- --4-3-2-3-4-- 4-3-2-1-2-3-4 --4-3-2-3-4-- ----4-3-4----
3 min read
Program to print half Diamond star pattern
Given an integer N, the task is to print half-diamond-star pattern. ************************************ Examples: Input: N = 3 Output: * ** *** ** * Input: N = 6 Output: * ** *** **** ***** ****** ***** **** *** ** * Approach: The idea is to break the pattern into two halves that is upper half and lower half. Then print them separately with the he
4 min read
Python Program to print hollow half diamond hash pattern
Give an integer N and the task is to print hollow half diamond pattern. Examples: Input : 6 Output : # # # # # # # # # # # # # # # # # # # # Input : 7 Output : # # # # # # # # # # # # # # # # # # # # # # # # Approach: The idea is to break the pattern into two parts: Upper part: For the upper half start the for loop with iterator (i) from 1 to n and
4 min read