# Finding the Frobenius Norm of a given matrix

• Difficulty Level : Easy
• Last Updated : 06 May, 2021

Given an M * N matrix, the task is to find the Frobenius Norm of the matrix. The Frobenius Norm of a matrix is defined as the square root of the sum of the squares of the elements of the matrix.
Example:

Input: mat[][] = {{1, 2}, {3, 4}}
Output: 5.47723
sqrt(12 + 22 + 32 + 42) = sqrt(30) = 5.47723
Input: mat[][] = {{1, 4, 6}, {7, 9, 10}}
Output: 16.8226

Approach: Find the sum of squares of the elements of the matrix and then print the square root of the calculated value.
Below is the implementation of the above approach:

## CPP

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `const` `int` `row = 2, col = 2;` `// Function to return the Frobenius``// Norm of the given matrix``float` `frobeniusNorm(``int` `mat[row][col])``{` `    ``// To store the sum of squares of the``    ``// elements of the given matrix``    ``int` `sumSq = 0;``    ``for` `(``int` `i = 0; i < row; i++) {``        ``for` `(``int` `j = 0; j < col; j++) {``            ``sumSq += ``pow``(mat[i][j], 2);``        ``}``    ``}` `    ``// Return the square root of``    ``// the sum of squares``    ``float` `res = ``sqrt``(sumSq);``    ``return` `res;``}` `// Driver code``int` `main()``{``    ``int` `mat[row][col] = { { 1, 2 }, { 3, 4 } };` `    ``cout << frobeniusNorm(mat);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``class` `GFG``{``    ` `    ``final` `static` `int` `row = ``2``, col = ``2``;``    ` `    ``// Function to return the Frobenius``    ``// Norm of the given matrix``    ``static` `float` `frobeniusNorm(``int` `mat[][])``    ``{``    ` `        ``// To store the sum of squares of the``        ``// elements of the given matrix``        ``int` `sumSq = ``0``;``        ``for` `(``int` `i = ``0``; i < row; i++)``        ``{``            ``for` `(``int` `j = ``0``; j < col; j++)``            ``{``                ``sumSq += (``int``)Math.pow(mat[i][j], ``2``);``            ``}``        ``}``    ` `        ``// Return the square root of``        ``// the sum of squares``        ``float` `res = (``float``)Math.sqrt(sumSq);``        ``return` `res;``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `main (String[] args)``    ``{``        ``int` `mat[][] = { { ``1``, ``2` `}, { ``3``, ``4` `} };``    ` `        ``System.out.println(frobeniusNorm(mat));``    ` `    ``}``}` `// This code is contributed by AnkitRai01`

## Python3

 `# Python3 implementation of the approach``from` `math ``import` `sqrt``row ``=` `2``col ``=` `2` `# Function to return the Frobenius``# Norm of the given matrix``def` `frobeniusNorm(mat):` `    ``# To store the sum of squares of the``    ``# elements of the given matrix``    ``sumSq ``=` `0``    ``for` `i ``in` `range``(row):``        ``for` `j ``in` `range``(col):``            ``sumSq ``+``=` `pow``(mat[i][j], ``2``)` `    ``# Return the square root of``    ``# the sum of squares``    ``res ``=` `sqrt(sumSq)``    ``return` `round``(res, ``5``)` `# Driver code` `mat ``=` `[ [ ``1``, ``2` `], [ ``3``, ``4` `] ]` `print``(frobeniusNorm(mat))` `# This code is contributed by mohit kumar 29`

## C#

 `// C# implementation of the approach``using` `System;` `class` `GFG``{``    ` `    ``static` `int` `row = 2, col = 2;``    ` `    ``// Function to return the Frobenius``    ``// Norm of the given matrix``    ``static` `float` `frobeniusNorm(``int` `[,]mat)``    ``{``    ` `        ``// To store the sum of squares of the``        ``// elements of the given matrix``        ``int` `sumSq = 0;``        ``for` `(``int` `i = 0; i < row; i++)``        ``{``            ``for` `(``int` `j = 0; j < col; j++)``            ``{``                ``sumSq += (``int``)Math.Pow(mat[i, j], 2);``            ``}``        ``}``    ` `        ``// Return the square root of``        ``// the sum of squares``        ``float` `res = (``float``)Math.Sqrt(sumSq);``        ``return` `res;``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main ()``    ``{``        ``int` `[,]mat = { { 1, 2 }, { 3, 4 } };``    ` `        ``Console.WriteLine(frobeniusNorm(mat));``    ``}``}` `// This code is contributed by AnkitRai01`

## Javascript

 ``
Output:
`5.47723`

My Personal Notes arrow_drop_up