Row and Column Sums of a Matrix
Programs with input · Advanced · about 15 min
Read the number of rows, then the number of columns, then each row of the matrix on its own line, with its whole numbers separated by spaces. Print three lines:
- the sum of each row, in order, as
Row sums: 6 15 24 - the sum of each column, in order, as
Column sums: 12 15 18 - the sum of the main diagonal (top left to bottom right) as
Diagonal sum: 15
A matrix with a different number of rows and columns has no main diagonal: then the last line is Diagonal sum: not a square matrix.
The prompts inside input() are not checked, only what you print.
Example
You type:
3 3 1 2 3 4 5 6 7 8 9
The program prints:
Row sums: 6 15 24 Column sums: 12 15 18 Diagonal sum: 15
rows = int(input("Rows: "))
cols = int(input("Columns: "))
# Read each row, then print the row sums, column sums and diagonal sum
The solution opens after your first Check.