Go Program to Find Sum of Each Row and Column of a Matrix

Write a Go Program to Find the Sum of Each Row and column of a Matrix. We used one for loop to find the sum of matrix rows and another loop to find the matrix’s sum.

package main

import "fmt"

func main() {
    var i, j, rows, columns int

    var rowColMatSum [10][10]int

    fmt.Print("Enter the Matrix rows and Columns = ")
    fmt.Scan(&rows, &columns)

    fmt.Println("Enter the Matrix Items to find the Row & Columns Sum = ")
    for i = 0; i < rows; i++ {
        for j = 0; j < columns; j++ {
            fmt.Scan(&rowColMatSum[i][j])
        }
    }
    for i = 0; i < rows; i++ {
        rowsum := 0
        for j = 0; j < columns; j++ {
            rowsum = rowsum + rowColMatSum[i][j]
        }
        fmt.Println("The Sum of Matrix ", i+1, " Row Item  = ", rowsum)
    }
    for i = 0; i < rows; i++ {
        colsum := 0
        for j = 0; j < columns; j++ {
            colsum = colsum + rowColMatSum[j][i]
        }
        fmt.Println("The Sum of Each Column Item in a Matrix  = ", colsum)
    }
}
Enter the Matrix rows and Columns = 2 2
Enter the Matrix Items to find the Row & Columns Sum = 
10 20
90 120
The Sum of Matrix  1  Row Item  =  30
The Sum of Matrix  2  Row Item  =  210
The Sum of Each Column Item in a Matrix  =  100
The Sum of Each Column Item in a Matrix  =  140

Golang Program to Find the Sum of Each Row and Column in a Matrix using the For loop range.

package main

import "fmt"

func main() {

    var rowColMatSum [3][3]int

    fmt.Println("Enter the Matrix Items to find the Row & Columns Sum = ")
    for i, rows := range rowColMatSum {
        for j := range rows {
            fmt.Scan(&rowColMatSum[i][j])
        }
    }
    for i, rows := range rowColMatSum {
        rowsum := 0
        for j := range rows {
            rowsum = rowsum + rowColMatSum[i][j]
        }
        fmt.Println("The Sum of Matrix ", i+1, " Row Item  = ", rowsum)
    }
    for i, rows := range rowColMatSum {
        colsum := 0
        for j := range rows {
            colsum = colsum + rowColMatSum[j][i]
        }
        fmt.Println("The Sum of Each Column Item in a Matrix  = ", colsum)
    }
}
Enter the Matrix Items to find the Row & Columns Sum = 
10 20 30
40 50 60
90 15 11
The Sum of Matrix  1  Row Item  =  60
The Sum of Matrix  2  Row Item  =  150
The Sum of Matrix  3  Row Item  =  116
The Sum of Each Column Item in a Matrix  =  140
The Sum of Each Column Item in a Matrix  =  85
The Sum of Each Column Item in a Matrix  =  101

In this Go example, we removed the extra for loop and placed the row and column sum in one loop to calculate the matrix row and column’s sum.

package main

import "fmt"

func main() {

    var rowColMatSum [3][3]int

    fmt.Println("Enter the Matrix Items to find the Row & Columns Sum = ")
    for i, rows := range rowColMatSum {
        for j := range rows {
            fmt.Scan(&rowColMatSum[i][j])
        }
    }
    for i, rows := range rowColMatSum {
        rowsum := 0
        colsum := 0
        for j := range rows {
            rowsum = rowsum + rowColMatSum[i][j]
            colsum = colsum + rowColMatSum[j][i]
        }
        fmt.Println("The Sum of Matrix ", i+1, " Row Item  = ", rowsum)
        fmt.Println("The Sum of Each Column Item in a Matrix  = ", colsum)
    }
}
Go Program to find Sum of each Row and Column of a Matrix 3