Here is a solution base on height equals to top to the middle, or half of the height. For example, height is entered as 4[7] or 5[9] below. This method will yield odd number actual height
h = int[input["please enter diamond's height:"]]
for i in range[h]:
print[" "*[h-i], "*"*[i*2+1]]
for i in range[h-2, -1, -1]:
print[" "*[h-i], "*"*[i*2+1]]
# please enter diamond's height:4
# *
# ***
# *****
# *******
# *****
# ***
# *
#
# 3, 2, 1, 0, 1, 2, 3 space
# 1, 3, 5, 7, 5, 3, 1 star
# please enter diamond's height:5
# *
# ***
# *****
# *******
# *********
# *******
# *****
# ***
# *
#
# 4, 3, 2, 1, 0, 1, 2, 3, 4 space
# 1, 3, 5, 7, 9, 7, 5, 3, 1 star
Here is another solution base on height equals to top to the bottom, or the actual total height. For example, height is entered as 7 or 9 below. When the user enters an even number for height, the diamond will be slightly slanted.
h = int[input["please enter diamond's height:"]]
for i in range[1, h, 2]:
print[" "*[h//2-i//2], "*"*i]
for i in range[h, 0, -2]:
print[" "*[h//2-i//2], "*"*i]
# please enter diamond's height:7
# *
# ***
# *****
# *******
# *****
# ***
# *
#
# 3, 2, 1, 0, 1, 2, 3 space
# 1, 3, 5, 7, 5, 3, 1 star
#
# please enter diamond's height:9
# *
# ***
# *****
# *******
# *********
# *******
# *****
# ***
# *
#
# 4, 3, 2, 1, 0, 1, 2, 3, 4 space
# 1, 3, 5, 7, 9, 7, 5, 3, 1 star
In this python program we first read row from user. Here row indicates number of rows that will be printed in one triangle pattern of Diamond pattern. Given row value of This python program prints Diamond pattern made up of stars up to n lines.
5
, total numbers
of line in pattern will be 9
.Python Source Code: Diamond Pattern
# Diamond pattern
# Reading number of row
row = int[input['Enter number of row: ']]
# Upper part of diamond
for i in range[1, row+1]:
for j in range[1,row-i+1]:
print[" ", end=""]
for j in range[1, 2*i]:
print["*", end=""]
print[]
# Lower part of diamond
for i in range[row-1,0, -1]:
for j in range[1,row-i+1]:
print[" ", end=""]
for j in range[1, 2*i]:
print["*", end=""]
print[]
Output
Enter number of row: 8
*
***
*****
*******
*********
***********
*************
***************
*************
***********
*********
*******
*****
***
*
View Discussion Improve Article Save Article View Discussion Improve Article Save Article Given a number n, write a program to print a diamond shape with 2n rows.
Examples
:
C++
#include
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
Javascript
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 0; i--]
{
for
[
var
j = 0; j < space; j++] document.write[
" "
];
for
[
var
j = 0; j < i; j++] document.write[
"*"
+
" "
];
document.write[
"
"];
space++;
}
}
printDiamond[5];
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.
Approach2: Solving the problem using Recursion
Implementation:
C++
#include
using
namespace
std;
void
gotonextLine[
int
k,
int
i,
int
z]
{
if
[k == i]
return
;
cout 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];
}
}
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 . 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.
Vote for difficulty
Current difficulty : Medium