Monday 12 January 2015

Java Program to find the normal of a matrix


This is a Java Program which calculates the normal of a given matrix.

Generally normal of a matrix is calculated as the root of sum of squares of all the elements in a matrix.
PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class NormalMatrix {
public static void main(String[] args) {
double sum = 0;
int rows, columns, i, j;
double normal;
Scanner scan = new Scanner(System.in);
System.out.print("Enter order of matrix : \t");
rows = scan.nextInt();
columns = scan.nextInt();
int matrix[][] = 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 m" + (i + 1) + (j + 1) + ":\t");
matrix[i][j] = scan.nextInt();
}
}
scan.close();
System.out.print("\n\nMATRIX is :\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++)
System.out.print(matrix[i][j] + "\t");

System.out.print("\n");
}
System.out.print("\n\nIts NORMAL is:\n");
for (j = 0; j < columns; j++) {
for (i = 0; i < rows; i++)
sum = sum + Math.pow(matrix[i][j], 2);

}
normal = Math.sqrt(sum);
System.out.print(normal);
}
}

OUTPUT :
Java - Normal of a matrix

No comments:

Post a Comment