In: Computer Science
Write a Python program which adds up columns and rows of given table as shown in the specified figure. Example test case (the four lines below “Input cell value” are input from user, and the five lines below “Results:” are the output of the program.):
Input number of rows/columns (0 to exit)
4
Input cell value:
25 69 51 26
68 35 29 54
54 57 45 63
61 68 47 59
Result:
25 69 51 26 171
68 35 29 54 186
54 57 45 63 219
61 68 47 59 235
208 229 172 202 811
Input number of rows/columns (0 to exit)
#Python code
while True:
N = int(input('Input number of rows/columns (0
to exit): '))
if N==0:
break
arr = [[0 for i in range(N+1)] for j in
range(N+1)]
print('Input cell value:')
for i in range(N):
for j in range(N):
arr[i][j] = int(input())
for i in range(N) :
for j in range(N)
:
arr[N][i] += arr[j][i]
for i in range(N+1) :
for j in range(N)
:
arr[i][N] += arr[i][j]
for i in range(N+1) :
for j in range(N+1)
:
print("%4d"%arr[i][j],end = ' ')
print()
#Screenshot