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

C++
#include <bits/stdc++.h>
using namespace std;
void printDiamond( int n)
{
int space = n - 1;
for ( int i = 0; i < n; i++)
{
for ( int j = 0;j < space; j++)
cout << " " ;
for ( int j = 0; j <= i; j++)
cout << "* " ;
cout << endl;
space--;
}
space = 0;
for ( int i = n; i > 0; i--)
{
for ( int j = 0; j < space; j++)
cout << " " ;
for ( int j = 0;j < i;j++)
cout << "* " ;
cout << endl;
space++;
}
}
int main()
{
printDiamond(5);
return 0;
}
|
C
#include<stdio.h>
void printDiamond( int n)
{
int space = n - 1;
for ( int i = 0; i < n; i++)
{
for ( int j = 0;j < space; j++)
printf ( " " );
for ( int j = 0;j <= i; j++)
printf ( "* " );
printf ( "\n" );
space--;
}
space = 0;
for ( int i = n; i > 0; i--)
{
for ( int j = 0; j < space; j++)
printf ( " " );
for ( int j = 0;j < i;j++)
printf ( "* " );
printf ( "\n" );
space++;
}
}
int main()
{
printDiamond(5);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void printDiamond( int n)
{
int space = n - 1 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < space; j++)
System.out.print( " " );
for ( int j = 0 ; j <= i; j++)
System.out.print( "* " );
System.out.print( "\n" );
space--;
}
space = 0 ;
for ( int i = n; i > 0 ; i--)
{
for ( int j = 0 ; j < space; j++)
System.out.print( " " );
for ( int j = 0 ; j < i; j++)
System.out.print( "* " );
System.out.print( "\n" );
space++;
}
}
public static void main(String[] args)
{
printDiamond( 5 );
}
}
|
Python3
def Diamond(rows):
n = 1
for i in range ( 1 , rows + 1 ):
for j in range ( 1 , (rows - i) + 1 ):
print (end = " " )
while n ! = (i + 1 ):
print ( "*" , end = " " )
n = n + 1
n = 1
print ()
k = 0
n = 0
for i in range ( 1 , rows + 1 ):
for j in range ( 1 , k + 1 ):
print (end = " " )
k = k + 1
while n < = (rows - i):
print ( "*" , end = " " )
n = n + 1
n = 0
print ()
rows = 5
Diamond(rows)
|
C#
using System;
class GFG
{
static void printDiamond( int n)
{
int space = n - 1;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < space; j++)
Console.Write( " " );
for ( int j = 0; j <= i; j++)
Console.Write( "* " );
Console.Write( "\n" );
space--;
}
space = 0;
for ( int i = n; i > 0; i--)
{
for ( int j = 0; j < space; j++)
Console.Write( " " );
for ( int j = 0; j < i; j++)
Console.Write( "* " );
Console.Write( "\n" );
space++;
}
}
public static void Main()
{
printDiamond(5);
}
}
|
PHP
<?php
function printDiamond( $n )
{
$space = $n - 1;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $space ; $j ++)
printf( " " );
for ( $j = 0; $j <= $i ; $j ++)
printf( "* " );
printf( "\n" );
$space --;
}
$space = 0;
for ( $i = $n ; $i > 0; $i --)
{
for ( $j = 0; $j < $space ; $j ++)
printf( " " );
for ( $j = 0; $j < $i ; $j ++)
printf( "* " );
printf( "\n" );
$space ++;
}
}
printDiamond(5);
?>
|
Javascript
<script>
function printDiamond(n) {
var space = n - 1;
for ( var i = 0; i < n; i++) {
for ( var j = 0; j < space; j++) document.write( " " );
for ( var j = 0; j <= i; j++) document.write( "*" + " " );
document.write( "<br>" );
space--;
}
space = 0;
for ( var i = n; i > 0; i--)
{
for ( var j = 0; j < space; j++) document.write( " " );
for ( var j = 0; j < i; j++) document.write( "*" + " " );
document.write( "<br>" );
space++;
}
}
printDiamond(5);
</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++
#include <bits/stdc++.h>
using namespace std;
void gotonextLine( int k, int i, int z)
{
if (k == i)
return ;
cout << "* " ;
gotonextLine(k + z, i, z);
}
void addblankSpaceInDiamond(
int j, int i, int z)
{
if (j == i)
return ;
cout << " " ;
addblankSpaceInDiamond(j + z, i, z);
}
void upperDiamond( int row, int i)
{
if (i > row)
return ;
addblankSpaceInDiamond(row, i, -1);
gotonextLine(0, i, 1);
cout << endl;
upperDiamond(row, i + 1);
}
void lowerDiamond( int row,
int i)
{
if (i > row)
return ;
addblankSpaceInDiamond(0, i, 1);
gotonextLine(row, i, -1);
cout << endl;
lowerDiamond(row, i + 1);
}
int main()
{
int row;
row = 5;
upperDiamond(row, 0);
lowerDiamond(row, 1);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void gotonextLine( int k, int i, int z)
{
if (k == i)
return ;
System.out.print( "* " );
gotonextLine(k + z, i, z);
}
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)
{
if (i > row)
return ;
addblankSpaceInDiamond(row, i, - 1 );
gotonextLine( 0 , i, 1 );
System.out.print( "\n" );
upperDiamond(row, i + 1 );
}
static void lowerDiamond( int row, int i)
{
if (i > row)
return ;
addblankSpaceInDiamond( 0 , i, 1 );
gotonextLine(row, i, - 1 );
System.out.print( "\n" );
lowerDiamond(row, i + 1 );
}
public static void main(String[] args)
{
int row;
row = 5 ;
upperDiamond(row, 0 );
lowerDiamond(row, 1 );
}
}
|
Python3
def gotonextLine(k, i, z):
if (k = = i):
return
print ( "* " , end = ""),
gotonextLine(k + z, i, z)
def addblankSpaceInDiamond(j,i,z):
if (j = = i):
return
print ( " " ,end = ""),
addblankSpaceInDiamond(j + z, i, z)
def upperDiamond(row,i):
if (i > row):
return
addblankSpaceInDiamond(row, i, - 1 )
gotonextLine( 0 , i, 1 )
print ( "\n" ,end = ""),
upperDiamond(row, i + 1 )
def lowerDiamond(row,i):
if (i > row):
return
addblankSpaceInDiamond( 0 , i, 1 )
gotonextLine(row, i, - 1 )
print ( "\n" ,end = ""),
lowerDiamond(row, i + 1 )
row = 5
upperDiamond(row, 0 )
lowerDiamond(row, 1 )
|
C#
using System;
public class GFG{
public static void gotonextLine( int k, int i, int z)
{
if (k == i)
return ;
Console.Write( "* " );
gotonextLine(k + z, i, z);
}
public static void addblankSpaceInDiamond(
int j, int i, int z)
{
if (j == i)
return ;
Console.Write( " " );
addblankSpaceInDiamond(j + z, i, z);
}
public static void upperDiamond( int row, int i)
{
if (i > row)
return ;
addblankSpaceInDiamond(row, i, -1);
gotonextLine(0, i, 1);
Console.Write( "\n" );
upperDiamond(row, i + 1);
}
public static void lowerDiamond( int row,
int i)
{
if (i > row)
return ;
addblankSpaceInDiamond(0, i, 1);
gotonextLine(row, i, -1);
Console.Write( "\n" );
lowerDiamond(row, i + 1);
}
public static void Main ()
{
int row;
row = 5;
upperDiamond(row, 0);
lowerDiamond(row, 1);
}
}
|
Javascript
function gotonextLine(k, i, z)
{
if (k == i)
return ;
console.log( "* " );
gotonextLine(k + z, i, z);
}
function addblankSpaceInDiamond(j, i, z)
{
if (j == i)
return ;
console.log( " " );
addblankSpaceInDiamond(j + z, i, z);
}
function upperDiamond(row, i)
{
if (i > row)
return ;
addblankSpaceInDiamond(row, i, -1);
gotonextLine(0, i, 1);
console.log( "<br>" );
upperDiamond(row, i + 1);
}
function lowerDiamond(row, i)
{
if (i > row)
return ;
addblankSpaceInDiamond(0, i, 1);
gotonextLine(row, i, -1);
console.log( "<br>" );
lowerDiamond(row, i + 1);
}
let row;
row = 5;
upperDiamond(row, 0);
lowerDiamond(row, 1);
|
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.
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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
18 Apr, 2023
Like Article
Save Article