write a program that print a shaped like diamond of numbers the program should accept a number and from user and display a diamond of dimension and then output should be (e.g. if N=4)
1
1 21
12321
1234321
12321
121
1
Answers
Answer:
# 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)
Python program to solve Numerical Diamond problem.
Language used: Python 3.0
Program:
n=int(input())
sp=n-1
l=[]
for i in range(1,n+1):
jj=''
kk=''
spp= ' ' * sp
print(spp,end='') #Starting spaces
for j in range(1,i+1): #first half elements of each row string 1234
print(j,end='')
jj=jj+str(j)
for k in range(1,i): #second half of each row's string [321]
print(k,end='')
kk=kk+str(k)
l.append(spp+jj+kk) #appending the complete row
#[spaces inc] to use them in printing next half of the diamond
print(end='\n')
sp=sp-1
#Printing the next half of the diamond
#As we dont need the middle row to be repeated, take n-1 rows only.
#And as they need to be printed in reverse order, reverse the list
l=l[:-1][::-1]
for i in l:
print(i)
Testcases:
Input1:
1
Output1:
1
Input2:
2
Output2:
1
121
1
Input3:
3
Output3:
1
121
12321
121
1
Input4:
4
Output4:
1
121
12321
1234321
12321
121
1
Learn more:
1. Hockey Problem
https://brainly.in/question/12306355
2. Numerical belly problem
https://brainly.in/question/16880104