122. JAVA Program to find Norm of the given matrix.
How does this program work?
- In this program we are going to learn about how to find Norm of the given matrix using java.
- Norm of a matrix is defined as square root of sum of squares of matrix elements.
- And then by using the formula we can find the norm of a matrix.
Here is the code
//To find Norm of the given matrix
import java.util.Scanner;public class Norm
{
public static void main(String[] args )
{
double sum = 0;
int rows, columns, i, j;
double norm;
System.out.print("Enter order of matrix : \t");
Scanner scan = new Scanner(System.in );
rows = scan.nextInt();
columns = scan.nextInt();
int mat[][] = new int[rows][columns];
System.out.print("Enter elements of the matrix :\n");
for ( i = 0; i < rows; i++ )
{
for ( j = 0; j < columns; j++ )
{
System.out.print("Enter element a:" + (i ) + (j) + ":\t");
mat[i][j] = scan.nextInt();
}
}
System.out.print("\n\nMATRIX is :\n" );
for ( i = 0; i < rows; i++ )
{
for ( j = 0; j < columns; j++ )
System.out.print(mat[i][j] + "\t" );
System.out.print("\n" );
}
System.out.print("\nNorm of matrix is:");
for ( j = 0; j < columns; j++ )
{
for ( i = 0; i < rows; i++ )
sum = sum + Math.pow ( mat [ i ][ j ] , 2 );
}
norm = Math.sqrt ( sum );
System.out.print(norm );
}
}