CSharp  Java  Python  Swift  GO  WPF  Ruby  Scala  F#  JavaScript  SQL  PHP  Angular  HTML
Program to find the sum of each row and each column of a matrixExplanationIn this program, we need to calculate the sum of elements in each row and each column of the given matrix. Above diagram shows the sum of elements of each row and each column of a matrix. Algorithm
SolutionPython#Initialize matrix a a = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; #Calculates number of rows and columns present in given matrix rows = len(a); cols = len(a[0]); #Calculates sum of each row of given matrix for i in range(0, rows): sumRow = 0; for j in range(0, cols): sumRow = sumRow + a[i][j]; print("Sum of " + str(i+1) +" row: " + str(sumRow)); #Calculates sum of each column of given matrix for i in range(0, rows): sumCol = 0; for j in range(0, cols): sumCol = sumCol + a[j][i]; print("Sum of " + str(i+1) +" column: " + str(sumCol)); Output: Sum of 1 row: 6 Sum of 2 row: 15 Sum of 3 row: 24 Sum of 1 column: 12 Sum of 2 column: 15 Sum of 3 column: 18 C#include <stdio.h> int main() { int rows, cols, sumRow, sumCol; //Initialize matrix a int a[][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; //Calculates number of rows and columns present in given matrix rows = (sizeof(a)/sizeof(a[0])); cols = (sizeof(a)/sizeof(a[0][0]))/rows; //Calculates sum of each row of given matrix for(int i = 0; i < rows; i++){ sumRow = 0; for(int j = 0; j < cols; j++){ sumRow = sumRow + a[i][j]; } printf("Sum of %d row: %d\n", (i+1), sumRow); } //Calculates sum of each column of given matrix for(int i = 0; i < cols; i++){ sumCol = 0; for(int j = 0; j < rows; j++){ sumCol = sumCol + a[j][i]; } printf("Sum of %d column: %d\n", (i+1), sumCol); } return 0; } Output: Sum of 1 row: 6 Sum of 2 row: 15 Sum of 3 row: 24 Sum of 1 column: 12 Sum of 2 column: 15 Sum of 3 column: 18 JAVApublic class SumofRowColumn { public static void main(String[] args) { int rows, cols, sumRow, sumCol; //Initialize matrix a int a[][] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; //Calculates number of rows and columns present in given matrix rows = a.length; cols = a[0].length; //Calculates sum of each row of given matrix for(int i = 0; i < rows; i++){ sumRow = 0; for(int j = 0; j < cols; j++){ sumRow = sumRow + a[i][j]; } System.out.println("Sum of " + (i+1) +" row: " + sumRow); } //Calculates sum of each column of given matrix for(int i = 0; i < cols; i++){ sumCol = 0; for(int j = 0; j < rows; j++){ sumCol = sumCol + a[j][i]; } System.out.println("Sum of " + (i+1) +" column: " + sumCol); } } } Output: Sum of 1 row: 6 Sum of 2 row: 15 Sum of 3 row: 24 Sum of 1 column: 12 Sum of 2 column: 15 Sum of 3 column: 18 C#using System; public class SumofRowColumn { public static void Main() { int rows, cols, sumRow, sumCol; //Initialize matrix a int[,] a = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; //Calculates number of rows and columns present in given matrix rows = a.GetLength(0); cols = a.GetLength(1); //Calculates sum of each row of given matrix for(int i = 0; i < rows; i++){ sumRow = 0; for(int j = 0; j < cols; j++){ sumRow = sumRow + a[i,j]; } Console.WriteLine("Sum of " + (i+1) +" row: " + sumRow); } //Calculates sum of each column of given matrix for(int i = 0; i < cols; i++){ sumCol = 0; for(int j = 0; j < rows; j++){ sumCol = sumCol + a[j,i]; } Console.WriteLine("Sum of " + (i+1) +" column: " + sumCol); } } } Output: Sum of 1 row: 6 Sum of 2 row: 15 Sum of 3 row: 24 Sum of 1 column: 12 Sum of 2 column: 15 Sum of 3 column: 18 PHP<!DOCTYPE html> <html> <body> <?php //Initialize matrix a $a = array( array(1, 2, 3), array(4, 5, 6), array(7, 8, 9) ); //Calculates number of rows and columns present in given matrix $rows = count($a); $cols = count($a[0]); //Calculates sum of each row of given matrix for($i = 0; $i < $rows; $i++){ $sumRow = 0; for($j = 0; $j < $cols; $j++){ $sumRow = $sumRow + $a[$i][$j]; } print("Sum of " . ($i+1) ." row: " . $sumRow); print("<br>"); } //Calculates sum of each column of given matrix for($i = 0; $i < $cols; $i++){ $sumCol = 0; for($j = 0; $j < $rows; $j++){ $sumCol = $sumCol + $a[$j][$i]; } print("Sum of " . ($i+1) . " column: " . $sumCol); print("<br>"); } ?> </body> </html> Output: Sum of 1 row: 6 Sum of 2 row: 15 Sum of 3 row: 24 Sum of 1 column: 12 Sum of 2 column: 15 Sum of 3 column: 18
Next Topic#
