Question #228698

Diamond

Given an integer value 

N, write a program to print a number diamond of 2*N -1 rows as shown below.

Input

The first line of input is an integer 

N.

Explanation

In the given example, the number of rows in the diamond is 

5.

So, the output should be

. . . . 0 . . . . 
. . . 0 0 0 . . . 
. . 0 0 0 0 0 . . 
. 0 0 0 0 0 0 0 . 
0 0 0 0 0 0 0 0 0
. 0 0 0 0 0 0 0 . 
. . 0 0 0 0 0 . . 
. . . 0 0 0 . . . 
. . . . 0 . . . .
Sample Input 1
5
Sample Output 1
. . . . 0 . . . . 
. . . 0 0 0 . . . 
. . 0 0 0 0 0 . . 
. 0 0 0 0 0 0 0 . 
0 0 0 0 0 0 0 0 0 
. 0 0 0 0 0 0 0 . 
. . 0 0 0 0 0 . . 
. . . 0 0 0 . . . 
. . . . 0 . . . .
Sample Input 2
4
Sample Output 2
. . . 0 . . . 
. . 0 0 0 . . 
. 0 0 0 0 0 . 
0 0 0 0 0 0 0 
. 0 0 0 0 0 . 
. . 0 0 0 . . 
. . . 0 . . .

Expert's answer

def diamon_shape(r):
    n = 0
    for i in range(1, r + 1):
        for j in range (1, (r - i) + 1):
            print(end = " ")
         
        while n != (2 * i - 1):
            print("0", end = "")
            n = n + 1
        n = 0
        print()
 
    k = 1
    n = 1
    for i in range(1, r):
        for j in range (1, k + 1):
            print(end = " ")
        k = k + 1
        while n <= (2 * (r - i) - 1):
            print("0", end = "")
            n = n + 1
        n = 1
        print()
 
N=int(input())
diamon_shape(N)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS