Write a Go Program to perform the Scalar Matrix Multiplication, which means multiplying each matrix item with a given number. Within the nested for loop, we are multiplying every Matrix item with user given number.
package main import "fmt" func main() { var i, j, rows, columns, num int var orgMat [10][10]int var scalarMultiMat [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(&orgMat[i][j]) } } fmt.Print("Enter the Scalar Matrix Multiplication Value = ") fmt.Scan(&num) fmt.Println("*** The Result of Scalar Matrix Multiplication ***") for i = 0; i < rows; i++ { for j = 0; j < columns; j++ { scalarMultiMat[i][j] = num * orgMat[i][j] fmt.Print(scalarMultiMat[i][j], " ") } fmt.Println() } }
Enter the Matrix rows and Columns = 2 2
Enter the Matrix Items to find the Columns Sum =
10 20
30 55
Enter the Scalar Matrix Multiplication Value = 5
*** The Result of Scalar Matrix Multiplication ***
50 100
150 275
Golang Program to perform Scalar Matrix Multiplication using For loop range.
package main import "fmt" func main() { var num int var orgMat [3][3]int var scalarMultiMat [3][3]int fmt.Println("Enter the Matrix Items to find the Columns Sum = ") for i, rows := range orgMat { for j := range rows { fmt.Scan(&orgMat[i][j]) } } fmt.Print("Enter the Scalar Matrix Multiplication Value = ") fmt.Scan(&num) fmt.Println("*** The Result of Scalar Matrix Multiplication ***") for i, rows := range orgMat { for j := range rows { scalarMultiMat[i][j] = num * orgMat[i][j] fmt.Print(scalarMultiMat[i][j], " ") } fmt.Println() } }