Java Program to Find the Trace and Normal of a Matrix

«
»
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.

advertisement
  1. import java.util.*;
  2. public class Trace
  3. {
  4.     public static void main(String args[])
  5.     {
  6.         int array[][]=new int[10][10];
  7.         int i, j;
  8.         double sum = 0, square = 0, result = 0;
  9.   	System.out.println("Enter total rows and columns: ");
  10.   	Scanner s = new Scanner(System.in);
  11.   	int row = s.nextInt();
  12.   	int column = s.nextInt();
  13. 	System.out.println("Enter matrix:");
  14.  	for(i = 0; i < row; i++)
  15.   	{
  16.    	    for(j = 0; j < column; j++) 
  17.      	    {
  18.       	        array[i][j] = s.nextInt();
  19.                 System.out.print(" ");
  20.      	    }
  21.         }
  22. 	System.out.println("The entered Matrix is :");
  23.  	for(i = 0; i < row; i++)
  24.         {
  25.       	    for(j = 0; j < column; j++)
  26.             {
  27.          	System.out.print(array[i][j]+" ");
  28.             }
  29.             System.out.println(" ");
  30.     	}
  31.         System.out.println("The Trace of the above matrix is ");
  32.   	for(i = 0; i < row; i++)
  33.   	{  
  34.     	    for(j = 0; j < column; j++)
  35.        	    {
  36.                 if(i == j)
  37.             	 {
  38.                	     sum = sum + (array[i][j]);
  39.                	 }
  40.             }
  41.         }
  42.         System.out.println(sum);  
  43.         System.out.println("The Normal of the above matrix is "); 
  44.    	for(i = 0; i < row; i++)
  45.    	{
  46.     	    for(j = 0; j < column; j++)
  47.        	    {
  48.        	        square = square + (array[i][j])*(array[i][j]);
  49.             }
  50.     	}
  51.         result = Math.sqrt(square);
  52.         System.out.println(result);
  53.     }
  54. }

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.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.