Write a Go Program to Find the Sum of Each Matrix Column using the for loop. It allows entering the rows, columns, and matrix items. Next, it finds the sum of each column in a given matrix.
package main import "fmt" func main() { var i, j, rows, columns int var columnSumMat [10][10]int fmt.Print("Enter the Matrix rows and Columns = ") fmt.Scan(&rows, &columns) fmt.Println("Enter the Matrix Items to find the Columns Sum = ") for i = 0; i < rows; i++ { for j = 0; j < columns; j++ { fmt.Scan(&columnSumMat[i][j]) } } for i = 0; i < rows; i++ { colsum := 0 for j = 0; j < columns; j++ { colsum = colsum + columnSumMat[j][i] } fmt.Println("The Sum of Matrix Column Elements = ", colsum) } }
Enter the Matrix rows and Columns = 2 2
Enter the Matrix Items to find the Columns Sum =
10 20
30 70
The Sum of Matrix Column Elements = 40
The Sum of Matrix Column Elements = 90
Golang Program to Find the Sum of Each Column in a Matrix using For loop range.
package main import "fmt" func main() { var columnSumMat [3][3]int fmt.Println("Enter the Matrix Items to find the Column Sum = ") for i, rows := range columnSumMat { for j := range rows { fmt.Scan(&columnSumMat[i][j]) } } for i, rows := range columnSumMat { colsum := 0 for j := range rows { colsum = colsum + columnSumMat[j][i] } fmt.Println("The Sum of Matrix Column Elements = ", colsum) } }