Go Program to Perform Arithmetic Operations on Matrix

Write a Go Program to Perform Arithmetic Operations on Matrix. This Go example allows entering the matrix rows, columns, and matrix items. Next, it performs the arithmetic operations such as addition, subtraction, multiplication, division, and remainder.

package main

import "fmt"

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

    var gomat1 [10][10]int
    var gomat2 [10][10]int

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

    fmt.Print("Enter the First Matrix Items  = ")
    for i = 0; i < rows; i++ {
        for j = 0; j < columns; j++ {
            fmt.Scan(&gomat1[i][j])
        }
    }

    fmt.Print("Enter the Second Matrix Items  = ")
    for i = 0; i < rows; i++ {
        for j = 0; j < columns; j++ {
            fmt.Scan(&gomat2[i][j])
        }
    }

    fmt.Println("Add\tSub\tMul\tDiv\tMod")
    for i = 0; i < rows; i++ {
        for j = 0; j < columns; j++ {
            fmt.Print("\n", gomat1[i][j]+gomat2[i][j], "\t")
            fmt.Print(gomat1[i][j]-gomat2[i][j], "\t")
            fmt.Print(gomat1[i][j]*gomat2[i][j], "\t")
            fmt.Print(gomat1[i][j]/gomat2[i][j], "\t")
            fmt.Print(gomat1[i][j]%gomat2[i][j], "\t")
        }
    }
    fmt.Println()
}
Enter the Matrix Rows and Columns = 2 2
Enter the First Matrix Items  = 
10 20
30 40
Enter the Second Matrix Items  = 
9 45
10 7
Add     Sub     Mul     Div     Mod

19      1       90      1       1
65      -25     900     0       20
40      20      300     3       0
47      33      280     5       5

This Go Program uses the For Loop Range to Perform Arithmetic Operations on Matrix. Here, we used the for loop range to allow the user to enter matrix items and perform arithmetic operations.

package main

import "fmt"

func main() {

    var gomat1 [2][3]int
    var gomat2 [2][3]int

    fmt.Print("Enter the First Matrix Items  = ")
    for k, r := range gomat1 {
        for l := range r {
            fmt.Scan(&gomat1[k][l])
        }
    }

    fmt.Print("Enter the Second Matrix Items  = ")
    for m, rr := range gomat2 {
        for n := range rr {
            fmt.Scan(&gomat2[m][n])
        }
    }

    fmt.Println("Add\tSub\tMul\tDiv\tMod")
    for i, rows := range gomat1 {
        for j := range rows {
            fmt.Print("\n", gomat1[i][j]+gomat2[i][j], "\t")
            fmt.Print(gomat1[i][j]-gomat2[i][j], "\t")
            fmt.Print(gomat1[i][j]*gomat2[i][j], "\t")
            fmt.Print(gomat1[i][j]/gomat2[i][j], "\t")
            fmt.Print(gomat1[i][j]%gomat2[i][j], "\t")
        }
    }
    fmt.Println()
}
Golang Program to Perform Arithmetic Operations on Matrix 2