This is a Java Program to Find the Trace & Normal of a given Matrix. The Trace of a matrix is the sum of main diagonal whereas the Normal is the square root of the sum of squares of all elements of a matrix.
Enter all the elements of matrix as input. We find the trace and normal of matrix using loops.
Here is the source code of the Java Program to Find the Trace & Normal of a given Matrix. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.*;
public class Trace
{
public static void main(String args[])
{
int array[][]=new int[10][10];
int i, j;
double sum = 0, square = 0, result = 0;
System.out.println("Enter total rows and columns: ");
Scanner s = new Scanner(System.in);
int row = s.nextInt();
int column = s.nextInt();
System.out.println("Enter matrix:");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
array[i][j] = s.nextInt();
System.out.print(" ");
}
}
System.out.println("The entered Matrix is :");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
System.out.print(array[i][j]+" ");
}
System.out.println(" ");
}
System.out.println("The Trace of the above matrix is ");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
if(i == j)
{
sum = sum + (array[i][j]);
}
}
}
System.out.println(sum);
System.out.println("The Normal of the above matrix is ");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
square = square + (array[i][j])*(array[i][j]);
}
}
result = Math.sqrt(square);
System.out.println(result);
}
}
Output:
$ javac Trace.java $ java Trace Enter total rows and columns: 3 3 Enter matrix: 1 2 3 4 5 6 7 8 9 The entered Matrix is : 1 2 3 4 5 6 7 8 9 The Trace of the above matrix is 15.0 The Normal of the above matrix is 16.881943016134134
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Practice Programming MCQs
- Practice Information Technology MCQs
- Practice BCA MCQs
- Apply for Java Internship
- Apply for Computer Science Internship