w3resource

Python: Adds up columns and rows of given table as shown in the specified figure


Sum of Rows and Columns

Your task is to develop a small part of spreadsheet software.
Write a Python program that adds up the columns and rows of the given table as shown in the specified figure:

Pictorial Presentation:

Python: Adds up columns and rows of given table as shown in the specified figure

n (the size of row and column of the given table)
1st row of the table
2nd row of the table
:
:
n th row of the table
The input ends with a line consisting of a single 0.
Output:
For each dataset, print the table with sum of rows and columns.

Sample Solution:

Python Code:

# Continuous loop to execute the program until the user chooses to exit (input 0)
while True:
    # Print statement to instruct the user to input the number of rows/columns (0 to exit)
    print("Input number of rows/columns (0 to exit)")
    
    # Read the input for the number of rows/columns
    n = int(input())
    
    # Check if the user wants to exit the program
    if n == 0:
        break
    
    # Print statement to instruct the user to input cell values
    print("Input cell value:")
    
    # Initialize an empty list to store the matrix
    x = []
    
    # Loop to input the matrix values
    for i in range(n):
        x.append([int(num) for num in input().split()])

    # Loop to calculate and append the row sums to each row
    for i in range(n):
        sum = 0
        for j in range(n):
            sum += x[i][j]
        x[i].append(sum)

    # Append an empty list to represent the additional row for column sums
    x.append([])
    
    # Loop to calculate and append the column sums to the additional row
    for i in range(n + 1):
        sum = 0
        for j in range(n):
            sum += x[j][i]
        x[n].append(sum)
    
    # Print statement to display the result
    print("Result:")
    
    # Loop to print the matrix with row and column sums
    for i in range(n + 1):
        for j in range(n + 1):
            print('{0:>5}'.format(x[i][j]), end="")
        print()

Sample Output:

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)

Explanation:

Here is a breakdown of the above Python code:

  • Continuous loop to execute the program until the user chooses to exit (input 0).
  • Print a statement to instruct the user to input the number of rows/columns (0 to exit).
  • Read the input for the number of rows/columns.
  • Check if the user wants to exit the program (input 0).
  • Print a statement to instruct the user to input cell values.
  • Initialize an empty list to store the matrix.
  • Loop to input the matrix values.
  • Loop to calculate and append the row sums to each row.
  • Append an empty list to represent the additional row for column sums.
  • Loop to calculate and append the column sums to the additional row.
  • Print a statement to display the result.
  • Loop to print the matrix with row and column sums.

Flowchart:

Flowchart: Python - Adds up columns and rows of given table as shown in the specified figure

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to find the number of combinations that satisfy p + q + r + s = n where n is a given number <= 4000 and p, q, r, s in the range of 0 to 1000
Next: Given a list of numbers and a number k, write a Python program to check whether the sum of any two numbers from the list is equal to k or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.