Python Program to Print a Number Diamond

 

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
3

Sample Output

. . 0 . . 
. 0 0 0 . 
0 0 0 0 0 
. 0 0 0 . 
. . 0 . . 

Code:

n=int(input())
for row in range(n):
    print(". "*(n-row-1),end="")
    print("0 "*(row+1),end="")
    print("0 "*(row),end="")
    print(". "*(n-row-1),end="")
    print()
for row in range(n-1,0,-1):
    print(". "*(n-row),end="")
    print("0 "*(row),end="")
    print("0 "*(row-1),end="")
    print(". "*(n-row),end="")
    print()

Input
8

Output

. . . . . . . 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 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 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 . . . . . . . 

Comments

Popular posts from this blog

CODING ASSIGNMENT 4

CODING PRACTICE 11 Music Page

CCBP Static Website Coding Assignment 2 Solution | Yoga Page