Python Online Compiler
Example: Python Program to Find the Sum of Each Row and Each Column of a Matrix using While Loop
C
C++
C#
Java
Python
PHP
main.py
STDIN
Run
# 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
4 3 12 23 12 34 12 43 34 53 12 56 34 23
Output
Clear
ADVERTISEMENTS