# Normal and Trace of a Matrix in Java

## Normal of a matrix

The square root of the total squares of each element in a matrix is known as the matrix's normal. Think of the following matrix as an example.

Example:

[[1,2,3]

[4,5,6]

[7,8,9]]

sumofSqures=1*1+2*2+3*3+4*4+5*5+6*6+7*7+8*8+9*9=

sumofSqures=385

Now find the square root of the number of sumofSqures.

Normal=sqrt(385)

Normal=16.8

## A Matrix trace

All the components that make up a matrix's major diagonal are added to form the trace (upper left to lower right). You should be aware that matrices must be square (Matrix should consist of an equal number of rows and columns). Solutions in linear algebra are useful to prove.

[[1,2,3]

[4,5,6]

[7,8,9]]

TraceofMatrix=1+5+9

TraceofMatrix=15

### Java Program for normal and a trace of the matrix

NormalandTrace.java

``````//this program is for finding the normal and the trace of the matrix
//importing the packages required
import java.util.*;
public class NormalandTrace
{
public static void main(String args[])
{
int arr[][] = new int;
int i, j;
double total = 0, sq = 0, res = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Please enter the num of rows: ");
// reading the integer value as rows from the user
int r= sc.nextInt();
System.out.print("Please enter the num of columns: ");
// reading the integer value as columns from the user
int c = sc.nextInt();
System.out.println("Enter matrix:");
//loop for iterating the rows
for(i = 0; i < r; i++)
{
//loop for iterating columns
for(j = 0; j < c; j++)
{
//reading the elements in the matrix
arr[i][j] = sc.nextInt();
// the space is printed
System.out.print(" ");
}
}
System.out.println("Displaying the entered matrix: ");
// displaying the entered integers as the matrix
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println(" ");
}
System.out.println("Displaying the trace of the matrix:");
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
//the condition for checking the element has i==j
if(i == j)
{
// the sum of the elements of the principal diagonal
total = total + (arr[i][j]);
}
}
}
//printing the trace of the matrix
System.out.println(total);
System.out.println("The result of the normal is: ");

for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
//th sum of the squares of the elements in the matrix
sq = sq + (arr[i][j])*(arr[i][j]);
}
}
// finding the square root of the number(sq)
res = Math.sqrt(sq);
// printing the result as the normal of the matrix
System.out.println(res);
}
}
``````

Output

``````Please enter the num of rows: 3
Please enter the num of columns: 3
Enter matrix:
1
2
3
4
5
6
7
8
9
Displaying the entered matrix:
1 2 3
4 5 6
7 8 9
Displaying the trace of the matrix:
15.0
The result of the normal is:
16.881943016134134
``````

NormalTrace2.java

``````//this program is for finding the normal and the trace of the matrix
//importing the packages required
import java.io.*;
public class NormalTrace2
{
// declaring the maximum size of the matrix
static int MAX = 10;
// the method can calculate and return the normal of the matrix
static int findNormal(int m[][], int num)
{
//storing the sumofsquares in the total variable
int total = 0;
for (int i=0; i<num; i++)
for (int j=0; j<num; j++)
total = total + m[i][j]*m[i][j];
return (int)Math.sqrt(total);
}
// method for finding the trace of the matrix
static int findTrace(int m[][], int num)
{
//storing the result in total variable
int total = 0;
for (int i=0; i<num; i++)
total = total + m[i][i];
}
//main section of the program
public static void main (String args[])
{
// static way of representing elements in the matrix
int m[][] = {{5, 3, 24, 1, 45},
{27, 8, 33, 15, 9},
{1, 11, 10, 5, 5},
{45, 18, 3, 9, 14},
{19, 3, 3, 13, 4},
};
//printing the normal and trace of the matrix m[][]

System.out.println ("The Trace for the given matrix is: "+ findTrace(m, 5));
System.out.println ("The result of Normal is: "+ findNormal(m, 5));
}
}
``````

Output

``````The Trace for the given matrix is: 36
The result of Normal is: 91
``````