44 lines
1.1 KiB
Java
44 lines
1.1 KiB
Java
import java.util.Scanner;
|
|
|
|
class TraceNorm {
|
|
|
|
public static void main(String[] args) {
|
|
Scanner sc = new Scanner(System.in);
|
|
|
|
System.out.print("Enter the size of the square matrix: ");
|
|
int n = sc.nextInt();
|
|
|
|
double[][] matrix = new double[n][n];
|
|
|
|
System.out.println("Enter the elements of the matrix:");
|
|
for (int i = 0; i < n; i++) {
|
|
for (int j = 0; j < n; j++) {
|
|
System.out.print(
|
|
"Enter the element at (" + i + "," + j + "): "
|
|
);
|
|
matrix[i][j] = sc.nextInt();
|
|
}
|
|
}
|
|
|
|
// trace
|
|
double trace = 0;
|
|
for (int i = 0; i < n; i++) {
|
|
trace += matrix[i][i];
|
|
}
|
|
|
|
// norm
|
|
double normSquared = 0;
|
|
for (int i = 0; i < n; i++) {
|
|
for (int j = 0; j < n; j++) {
|
|
normSquared += matrix[i][j] * matrix[i][j];
|
|
}
|
|
}
|
|
double norm = Math.sqrt(normSquared);
|
|
|
|
System.out.println("Trace of the matrix: " + trace);
|
|
System.out.println("Norm of the matrix: " + norm);
|
|
|
|
sc.close();
|
|
}
|
|
}
|