# Program to print the diamond shape

• Difficulty Level : Medium
• Last Updated : 29 Jun, 2022

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

## C++

 `// C++ program to print diamond shape``// with 2n rows``#include ``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` `// 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 ``=` `0``    ``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 !``=` `(``2` `*` `i ``-` `1``):``            ``print``(``"*"``, end ``=` `"")``            ``n ``=` `n ``+` `1``        ``n ``=` `0``        ` `        ``# line break``        ``print``()` `    ``k ``=` `1``    ``n ``=` `1``    ``for` `i ``in` `range``(``1``, rows):``        ``# loop to print spaces``        ``for` `j ``in` `range` `(``1``, k ``+` `1``):``            ``print``(end ``=` `" "``)``        ``k ``=` `k ``+` `1``        ` `        ``# loop to print star``        ``while` `n <``=` `(``2` `*` `(rows ``-` `i) ``-` `1``):``            ``print``(``"*"``, end ``=` `"")``            ``n ``=` `n ``+` `1``        ``n ``=` `1``        ``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

 ` 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

 ``

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.

Implementation:

## C++

 `// C++ program to print daimond pattern using recursion``#include ``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 uper part of triangle``    ``lowerDiamond(row, 1); ``// print lower part of diamond``    ``return` `0;``    ``// this code is contributed by Shivesh Kumar Dwivedi``}`

## 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 uper part of triangle``    ``lowerDiamond(row, 1); ``// print lower part of diamond``  ``}``}` `// This code is contributed by akashish__`

Output

```
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
```

This article is contributed by Rahul Singh(Nit KKR) and improved by Himanshu Patel(@prophet1999). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.