- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Golang Program To Find the Trace and Normal of a given Matrix

In this tutorial we will write an article to find the normal and trace of a matrix. A matrix is considered to be **normal** if its square root equals the sum of the squares of each member and **trace** is the total of a matrix's diagonal elements.

## Finding Normal of the given Matrix

### Algorithm

**STEP 1** − First we need to import the fmt and math package.

**STEP 2** − Create a function to find the Normal of the matrix. This function uses two for loops to find the square of each element.

**STEP 3** − Update the sum variable by adding the square of each element to it. Return the sum variable.

**STEP 4** − Start the main() function. Initialize a matrix and print it on the screen.

**STEP 5** − call the findNormal() function by passing the matrix as well as its rank to the function as arguments.

**STEP 6** − Store the result from the function in a new variable called normal and print it on the screen using fmt.Println() function.

### Example 1

Let us write a go program to find the normal of 2 X 2 matrix using for loops.

package main import ( "fmt" "math" ) // function to create normal func findNormal(mat [2][2]int, n int) float64 { var sum int = 0 for i := 0; i < n; i++ { for j := 0; j < n; j++ { sum += mat[i][j] * mat[i][j] } } var sum1 float64 sum1 = float64(sum) return math.Sqrt(sum1) } func main() { mat := [2][2]int{ {1, 2}, {5, 6}, } fmt.Println("The given matrix is: \n") for i := 0; i < 2; i++ { for j := 0; j < 2; j++ { fmt.Print(mat[i][j], "\t") } fmt.Println() } fmt.Println() normal := findNormal(mat, 2) fmt.Println("Normal of Matrix is:", normal) }

### Output

The given matrix is: 1 2 5 6 Normal of Matrix is: 8.12403840463596

### Example 2

In this example we will write a go program to find normal of a 3 X 3 matrix.

package main import ( "fmt" "math" ) func findNormal(mat [3][3]int, n int) float64 { var sum int = 0 for i := 0; i < n; i++ { for j := 0; j < n; j++ { sum += mat[i][j] * mat[i][j] } } var sum1 float64 sum1 = float64(sum) return math.Sqrt(sum1) } func main() { mat := [3][3]int{ {0, 1, 2}, {4, 5, 6}, {8, 9, 10}, } fmt.Println("The given matrix is: \n") for i := 0; i < 3; i++ { for j := 0; j < 3; j++ { fmt.Print(mat[i][j], "\t") } fmt.Println() } normal := findNormal(mat, 3) fmt.Println("\n Normal of Matrix is:", normal) }

### Output

The given matrix is: 0 1 2 4 5 6 8 9 10 Normal of Matrix is: 18.083141320025124

## Finding Trace of the given Matrix

### Algorithm

**STEP 1** − First we need to import the fmt and math package.

**STEP 2** − Create a function to find the tace of the matrix. This function uses a for loops to find the sum of diagonal elements.

**STEP 3** − Update the sum variable by adding each diagonal element to it. Return the sum variable.

**STEP 4** − Start the main() function. Initialize a matrix and print it on the screen.

**STEP 5** − call the findNormal() function by passing the matrix as well as its rank as arguments to function.

**STEP 6** − Store the result from the function in a new variable called trace and print it on the screen using fmt.Println() function.

### Example

Let us write a golang program to find the trace of a 2 X 2 matrix.

package main import ( "fmt" ) // function to create trace func findTrace(mat [2][2]int, n int) int { var sum int = 0 for i := 0; i < n; i++ { sum += mat[i][i] } return sum } func main() { mat := [2][2]int{ {10, 20}, {50, 60}, } fmt.Println("The given matrix is: \n") for i := 0; i < 2; i++ { for j := 0; j < 2; j++ { fmt.Print(mat[i][j], "\t") } fmt.Println() } trace := findTrace(mat, 2) fmt.Println("\nTrace of Matrix is:", trace) }

### Output

The given matrix is: 10 20 50 60 Trace of Matrix is: 70

## Conclusion

Successfully compiled and executed a golang program to find the normal and trace of matrices along with examples.

- Related Articles
- Golang Program To Find The Trace And Normal Of A Matrix
- Java Program To Find the Trace and Normal of a given Matrix
- C++ Program To Find the Trace and Normal of a given Matrix
- Swift Program to Find the Trace and Normal of a given Matrix
- C++ Program to Find the Normal and Trace
- Golang Program To Find The Transpose Of A Matrix
- Golang Program To Determine If a Given Matrix is a Sparse Matrix
- Maximum trace possible for any sub-matrix of the given matrix in C++
- Golang Program to find the parity of a given number.
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix in C++ Program
- Python Program to find out the determinant of a given special matrix
- Program to find the transpose of given matrix in Python
- Golang Program to Compute the Sum of Diagonals of a Matrix
- Golang Program to Print Boundary Elements of a Matrix
- Golang Program to Find the Area of a Triangle Given All Three Sides