Python Program to Find the Sum of Each Row and Each Column of a Matrix
Python program to find the sum of each row and each column of a matrix.
In this article, you will learn how to make a python program to find the sum of each row and each column of a matrix.
Example
-----Enter the number of rows & columns of the matrix-----
4
3
-----Enter the coefficients of the matrix-----
12
23
12
34
12
43
34
53
12
56
34
23
The Sum of the 0 position row is = 47
The Sum of the 1 position row is = 89
The Sum of the 2 position row is = 99
The Sum of the 3 position row is = 113
The Sum of the 0 position column is = 136
The Sum of the 1 position column is = 122
The Sum of the 2 position column is = 90
You should have knowledge of the following topics in python programming to understand these programs:
- Python
input()
function - Python For loop
Source Code
# Python Program to Find the Sum of Each Row and Each Column of a Matrix
print ("-----Enter the number of rows & columns of the matrix-----")
# These are the matrix's dimension
x = int (input ())
y = int (input ())
a, sum = [], 0
print ("\n-----Enter the coefficients of the matrix-----")
for i in range (x):
a.append([])
for j in range (y):
a[i].append(int (input ()))
print (end="\n")
# This statement will compute the
# sum of the matrix's rows elements
for i in range (x):
for j in range (y):
sum = sum + a[i][j]
print ("The Sum of the ", i, " position row is = ", sum)
sum = 0
print (end="\n")
sum = 0
# This statement will compute the
# sum of the matrix's columns elements
for j in range (y):
for i in range (x):
sum = sum + a[i][j]
print ("The Sum of the ", j, " position column is = ", sum)
sum = 0
Output
-----Enter the number of rows & columns of the matrix-----
4
3
-----Enter the co-efficients of the matrix-----
12
23
12
34
12
43
34
53
12
56
34
23
The Sum of the 0 position row is = 47
The Sum of the 1 position row is = 89
The Sum of the 2 position row is = 99
The Sum of the 3 position row is = 113
The Sum of the 0 position column is = 136
The Sum of the 1 position column is = 122
The Sum of the 2 position column is = 90
Explanation
In this given program, we have taken the inputs size of matrix rows and columns 4
and 3
then we collected coefficient values of the matrix from the user via the system console.
Then we applied for loop on this matrix twice a time to make the sum of its coefficients row-wise and column-wise.
Then this will return the final output of these sums row-wise 47
, 89
, 99
, and 113
& column-wise are 136
, 122
, and 90
of the above program.
Python Program to Find the Sum of Each Row and Each Column of a Matrix using While Loop
# Python Program to Find the Sum of Each Row and Each Column of a Matrix using While Loop
print ("-----Enter the number of rows & columns of the matrix-----")
# These are the matrix's dimension
x = int (input ())
y = int (input ())
a, sum = [], 0
print ("\n-----Enter the coefficients of the matrix-----")
i=0
while (i < x):
a.append([])
j=0
while (j < y):
a[i].append(int (input ()))
j=j+1
print (end="\n")
i=i+1
# This statement will compute the
# sum of the matrix's rows elements
i=0
while (i < x):
j=0
while (j < y):
sum = sum + a[i][j]
j=j+1
print ("The Sum of the ", i, " position row is = ", sum)
sum=0
i=i+1
print (end="\n")
sum=0
# This statement will compute the
# sum of the matrix's columns elements
j=0
while (j < y):
i=0
while (i < x):
sum = sum + a[i][j]
i=i+1
print ("The Sum of the ", j, " position column is = ", sum)
sum=0
j=j+1