# Golang Program to calculate the sum of columns of matrix elements

A matrix is a collection of numbers arranged in rows and columns, a two-dimensional array, each of the values of this matrix is known as an element. Here we will use three methods to find the sum of column elements and compare each method for the same using go programming.

Here is an example of a matrix and the sum value of it’s columns −

The given matrix is −

0  1  2
4  5  6
8  9  7


Sum of elements in column 1 is 12

Sum of elements in column 2 is 15

Sum of elements in column 3 is 15

## Algorithm

• Step 1 − Import the fmt package.

• Step 2 − Now we need to start the main() function.

• Step 3 − Then we are creating a matrix naming matrix and assign elements to it.

• Step 4 − Print the matrix on the screen using fmt.Println() function.

• Step 5 − Initialize a new variable called sum of type int to hold the resultant sum.

• Step 6 − To find sum of the column elements use the for loop to iterate over the matrix.

• Step 7 − Using the first for loop is used to get the column of the matrix while the second for loop gives us the column of the matrix.

• Step 8 − Once the loop gets over the matrix elements update the sum variable by adding values to it.

• Step 9 − Print the sum of the matrix on the screen.

## Example 1

In the following example, we will use a for loop to iterate over the matrix and find the sum of its elements and print it on the screen.

package main
import "fmt"
func main() {
matrix := [][]int{
{0, 1, 2},
{4, 5, 6},
{8, 9, 7},
}
fmt.Println("The given matrix is:")
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
fmt.Print(matrix[i][j], "\t")
}
fmt.Println()
}
fmt.Println()
for i := 0; i < len(matrix[0]); i++ {
sum := 0
for j := 0; j < len(matrix); j++ {
sum += matrix[j][i]
}
fmt.Printf("Sum of elements in column %d is %d\n", i+1, sum)
}
}


## Output

The given matrix is:
0	1	2
4	5	6
8	9	7

Sum of elements in column 1 is 12
Sum of elements in column 2 is 15
Sum of elements in column 3 is 15


## Example 2

In this example we will find the sum of the columns of matrix elements using range function.

package main
import "fmt"
func main() {
matrix := [][]int{
{10, 1, 2},
{4, 50, 6},
{8, 9, 7},
}
fmt.Println("The given matrix is:")
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
fmt.Print(matrix[i][j], "\t")
}
fmt.Println()
}
fmt.Println()
for i := 0; i < len(matrix[0]); i++ {
sum := 0
for _, row := range matrix {
sum += row[i]
}
fmt.Printf("Sum of elements in column %d is %d\n", i+1, sum)
}
}


## Output

The given matrix is:
10	1	2
4	50	6
8	9	7

Sum of elements in column 1 is 22
Sum of elements in column 2 is 60
Sum of elements in column 3 is 15


## Example 3

In this example we will use the recursion approach to find the sum of columns of matrix elements.

package main
import "fmt"
func colSum(matrix [][]int, col int) int {
if col == len(matrix[0]) {
return 0
}
sum := 0
for i := range matrix {
sum += matrix[i][col]
}
return sum
}
func main() {
matrix := [][]int{
{20, 1, 22},
{43, 5, 16},
{86, 91, 10},
}
fmt.Println("The given matrix is:")
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
fmt.Print(matrix[i][j], "\t")
}
fmt.Println()
}
fmt.Println()
for i := 0; i < len(matrix[0]); i++ {
fmt.Printf("Sum of elements in column %d is %d\n", i+1, colSum(matrix, i))
}
}


## Output

The given matrix is:
20	1	22
43	5	16
86	91	10

Sum of elements in column 1 is 149
Sum of elements in column 2 is 97
Sum of elements in column 3 is 48


## Conclusion

We have successfully compiled and executed a go language program to calculate the sum of columns of matrix elements along with examples. In the first and second example we have used for loop and range functions respectively in the main() section of the program while in the last example we have used a separate function to implement the logic.

Updated on: 16-Feb-2023

198 Views