Go Program to Find Sum of Matrix Lower Triangle

Write a Go Program to Find the Sum of the lower triangle items in a Matrix. In this Go sum of Matrix Lower Triangle example, we used the nested for loops to iterate the matrix row and column items. Within the loop, the if statement (if i > j) checks whether the row value is greater than the column. If True, add that item value to the matrix lower triangle sum.

package main

import "fmt"

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

    var ltriSumMat [10][10]int

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

    fmt.Println("Enter Matrix Items to find Lower Triangle Sum = ")
    for i = 0; i < rows; i++ {
        for j = 0; j < columns; j++ {
            fmt.Scan(&ltriSumMat[i][j])
        }
    }
    ltriSum := 0
    for i = 0; i < rows; i++ {
        for j = 0; j < columns; j++ {
            if i > j {
                ltriSum = ltriSum + ltriSumMat[i][j]
            }
        }
    }
    fmt.Println("The Sum of Lower Triangle Matrix Elements  = ", ltriSum)
}
Enter the Matrix rows and Columns = 3 3
Enter Matrix Items to find Lower Triangle Sum = 
10 20 30
40 50 60
70 80 90
The Sum of Lower Triangle Matrix Elements  =  190

Golang Program to Find the Sum of Lower Triangle items in a Matrix using For loop range.

package main

import "fmt"

func main() {

    var ltriSumMat [3][3]int

    fmt.Println("Enter Matrix Items to find Lower Triangle Sum = ")
    for i, rows := range ltriSumMat {
        for j := range rows {
            fmt.Scan(&ltriSumMat[i][j])
        }
    }
    ltriSum := 0
    for i, rows := range ltriSumMat {
        for j := range rows {
            if i > j {
                ltriSum = ltriSum + ltriSumMat[i][j]
            }
        }
    }
    fmt.Println("The Sum of Lower Triangle Matrix Elements  = ", ltriSum)
}
Golang Program to Find Sum of Matrix Lower Triangle 2