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

In this article, we will understand how to find the trace and normal of a given matrix. The normal of a matrix is the square root of the sum of squares of all the elements of a matrix. The trace of a matrix is the sum of all the elements present in the principal diagonal (upper left to lower right).

Below is a demonstration of the same −

Suppose our input is

The matrix is defined as:
2 3 4
5 2 3
4 6 9

The desired output would be

Trace value: 13.0
Normal value: 14.142135623730951

## Algorithm

Step 1 - START
Step 2 - Declare an integer matrix namely input_matrix
Step 3 - Define the values.
Step 4 - To compute trace value, iterate over each element of the matrix using two for-loops, add the diagonal elements and store the value.
Step 5 - To compute the normal value, iterate over each element of the matrix using two for-loops, compute the sum of square of each element, them compute the square root of the value and store the value.
Step 5 - Display the result
Step 6 - Stop

## Example 1

Here, we bind all the operations together under the ‘main’ function.

public class NormalAndTrace {
public static void main(String args[]) {
int[][] input_matrix = {
{2, 3, 4},
{5, 2, 3},
{4, 6, 9}
};
int i, j, matrix_size = 3;
double trace = 0, square = 0, normal = 0;
System.out.println("The matrix is defined as: ");
for(i = 0; i < matrix_size; i++) {
for(j = 0; j < matrix_size; j++)
System.out.print(input_matrix[i][j]+" ");
System.out.println(" ");
}
System.out.println("\nThe Trace value of the matrix is ");
for(i = 0; i < matrix_size; i++) {
for(j = 0; j < matrix_size; j++) {
if(i == j) {
trace = trace + (input_matrix[i][j]);
}
}
}
System.out.println(trace);
System.out.println("\nThe Normal value of the matrix is ");
for(i = 0; i < matrix_size; i++) {
for(j = 0; j < matrix_size; j++) {
square = square + (input_matrix[i][j])*(input_matrix[i][j]);
}
}
normal = Math.sqrt(square);
System.out.println(normal);
}
}

## Output

The matrix is defined as:
2 3 4
5 2 3
4 6 9

The Trace value of the matrix is
13.0

The Normal value of the matrix is
14.142135623730951

## Example 2

Here, we encapsulate the operations into functions exhibiting object-oriented programming.

public class NormalAndTrace {
static int matrix_size = 3;
static void normal_trace(int input_matrix[][]){
int i, j;
double trace = 0, square = 0, normal = 0;
System.out.println("\nThe Trace value of the matrix is ");
for(i = 0; i < matrix_size; i++) {
for(j = 0; j < matrix_size; j++) {
if(i == j) {
trace = trace + (input_matrix[i][j]);
}
}
}
System.out.println(trace);
System.out.println("\nThe Normal value of the matrix is ");
for(i = 0; i < matrix_size; i++) {
for(j = 0; j < matrix_size; j++) {
square = square + (input_matrix[i][j])*(input_matrix[i][j]);
}
}
normal = Math.sqrt(square);
System.out.println(normal);
}
public static void main(String args[]) {
int i, j;
int[][] input_matrix = { {2, 3, 4},
{5, 2, 3},
{4, 6, 9}
};
System.out.println("The matrix is defined as: ");
for(i = 0; i < matrix_size; i++) {
for(j = 0; j < matrix_size; j++)
System.out.print(input_matrix[i][j]+" ");
System.out.println(" ");
}
normal_trace(input_matrix);
}
}

## Output

The matrix is defined as:
2 3 4
5 2 3
4 6 9

The Trace value of the matrix is
13.0

The Normal value of the matrix is
14.142135623730951

Updated on: 29-Mar-2022

271 Views