Program to find Normal and Trace of a matrix

07 Jul, 2022
Given a 2D matrix, the task is to find Trace and Normal of matrix.Normal of a matrix is defined as square root of sum of squares of matrix elements.Trace of a n x n square matrix is sum of diagonal elements.
Examples : 
Input : mat[][] = {{7, 8, 9},
                   {6, 1, 2},
                   {5, 4, 3}};
Output : Normal = 16  
         Trace  = 11
Explanation : 
Normal = sqrt(7*7+ 8*8 + 9*9 + 6*6 +
              1*1 + 2*2 + 5*5 + 4*4 + 3*3)   
       = 16
Trace  = 7+1+3 = 11

Input :mat[][] = {{1, 2, 3},
                  {6, 4, 5},
                  {2, 1, 3}};
Output : Normal = 10  
         Trace = 8
Explanation : 
Normal = sqrt(1*1 +2*2 + 3*3 + 6*6 + 4*4 + 
             5*5 + 2*2 + 1*1 + 3*3)   
Trace = 8(1+4+3)
Implementation:
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find trace and normal// of given matrix#include<bits/stdc++.h>using namespace std; // Size of given matrixconst int MAX = 100; // Returns Normal of a matrix of size n x nint findNormal(int mat[][MAX], int n){    int sum = 0;    for (int i=0; i<n; i++)        for (int j=0; j<n; j++)            sum += mat[i][j]*mat[i][j];    return sqrt(sum);} // Returns trace of a matrix of size n x nint findTrace(int mat[][MAX], int n){    int sum = 0;    for (int i=0; i<n; i++)         sum += mat[i][i];    return sum;} // Driven sourceint main(){    int mat[][MAX] = {{1, 1, 1, 1, 1},        {2, 2, 2, 2, 2},        {3, 3, 3, 3, 3},        {4, 4, 4, 4, 4},        {5, 5, 5, 5, 5},    };    cout << "Trace of Matrix = "         << findTrace(mat, 5) << endl;    cout << "Normal of Matrix = "         << findNormal(mat, 5) << endl;    return 0;}
// Java program to find trace and normal// of given matrix import java.io.*; class GFG { // Size of given matrixstatic  int MAX = 100; // Returns Normal of a matrix of size n x n static int findNormal(int mat[][], int n){    int sum = 0;    for (int i=0; i<n; i++)        for (int j=0; j<n; j++)            sum += mat[i][j]*mat[i][j];    return (int)Math.sqrt(sum);} // Returns trace of a matrix of size n x n static int findTrace(int mat[][], int n){    int sum = 0;    for (int i=0; i<n; i++)        sum += mat[i][i];    return sum;} // Driven sourcepublic static void main (String[] args) {             int mat[][] = {{1, 1, 1, 1, 1},        {2, 2, 2, 2, 2},        {3, 3, 3, 3, 3},        {4, 4, 4, 4, 4},        {5, 5, 5, 5, 5},    };     System.out.println ("Trace of Matrix = "         + findTrace(mat, 5));    System.out.println ("Normal of Matrix = "         + findNormal(mat, 5));             }} // This code is contributed by vt_m.
# Python3 program to find trace and# normal of given matriximport math # Size of given matrixMAX = 100; # Returns Normal of a matrix# of size n x ndef findNormal(mat, n):     sum = 0;    for i in range(n):        for j in range(n):            sum += mat[i][j] * mat[i][j];    return math.floor(math.sqrt(sum)); # Returns trace of a matrix of# size n x ndef findTrace(mat, n):     sum = 0;    for i in range(n):        sum += mat[i][i];    return sum; # Driver Codemat = [[1, 1, 1, 1, 1],       [2, 2, 2, 2, 2],       [3, 3, 3, 3, 3],       [4, 4, 4, 4, 4],       [5, 5, 5, 5, 5]]; print("Trace of Matrix =", findTrace(mat, 5)); print("Normal of Matrix =", findNormal(mat, 5)); # This code is contributed by mits
// C# program to find trace and normal// of given matrixusing System; class GFG {         // Returns Normal of a matrix of    // size n x n    static int findNormal(int [,]mat, int n)    {        int sum = 0;                 for (int i = 0; i < n; i++)            for (int j = 0; j < n; j++)                sum += mat[i,j] * mat[i,j];                         return (int)Math.Sqrt(sum);    }         // Returns trace of a matrix of size    // n x n    static int findTrace(int [,]mat, int n)    {        int sum = 0;                 for (int i = 0; i < n; i++)            sum += mat[i,i];                     return sum;    }         // Driven source    public static void Main ()    {        int [,]mat = { {1, 1, 1, 1, 1},                       {2, 2, 2, 2, 2},                       {3, 3, 3, 3, 3},                       {4, 4, 4, 4, 4},                       {5, 5, 5, 5, 5},    };     Console.Write ("Trace of Matrix = "            + findTrace(mat, 5) + "\n");    Console.Write("Normal of Matrix = "                    + findNormal(mat, 5));             }} // This code is contributed by nitin mittal.
<?php// PHP program to find trace and// normal of given matrix // Size of given matrix$MAX = 100; // Returns Normal of a// matrix of size n x nfunction findNormal($mat, $n){    $sum = 0;    for ( $i = 0; $i < $n; $i++)        for ( $j = 0; $j < $n; $j++)            $sum += $mat[$i][$j] *                    $mat[$i][$j];    return floor(sqrt($sum));} // Returns trace of a// matrix of size n x nfunction findTrace( $mat, $n){    $sum = 0;    for ( $i = 0; $i < $n; $i++)        $sum += $mat[$i][$i];    return $sum;} // Driver Code$mat = array(array(1, 1, 1, 1, 1),             array(2, 2, 2, 2, 2),             array(3, 3, 3, 3, 3),             array(4, 4, 4, 4, 4),             array(5, 5, 5, 5, 5)); echo "Trace of Matrix = ",  findTrace($mat, 5),"\n"; echo "Normal of Matrix = " ,       findNormal($mat, 5) ; // This code is contributed by anuj_67.?>
<script> // Javascript program to find trace// and normal of given matrix // Size of given matrixvar MAX = 100; // Returns Normal of a matrix of size n x nfunction findNormal(mat, n){    var sum = 0;    for(var i = 0; i < n; i++)        for(var j = 0; j < n; j++)            sum += mat[i][j] * mat[i][j];                 return parseInt(Math.sqrt(sum));} // Returns trace of a matrix of size n x nfunction findTrace(mat, n){    var sum = 0;    for(var i = 0; i < n; i++)        sum += mat[i][i];             return sum;} // Driver codevar mat = [ [ 1, 1, 1, 1, 1 ],            [ 2, 2, 2, 2, 2 ],            [ 3, 3, 3, 3, 3 ],            [ 4, 4, 4, 4, 4 ],            [ 5, 5, 5, 5, 5 ] ]; document.write("Trace of Matrix = " +               findTrace(mat, 5) + "<br>");document.write("Normal of Matrix = " +               findNormal(mat, 5)); // This code is contributed by Kirti </script>
Trace of Matrix = 15
Normal of Matrix = 16
Time Complexity : O(n*n) Space Complexity : O(1)
This article is contributed by DANISH_RAZA. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 
nitin mittal
vt_m
Mithun Kumar
Kirti_Mangal
hardikkoriintern
Mathematical
Matrix
Mathematical
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.