Java Online Compiler
Example: Program to Find Normal and Trace of a Matrix in Java using For loop
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Program to Find Normal and Trace of a Matrix in Java using For loop import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int i, j, n, sum = 0, a = 0; double sum1 = 0, normal; System.out.println ("Enter the number of rows[columns] of the matrix::\n"); n = in.nextInt(); int x[][]; x = new int[n][n]; System.out.println ("\n----Enter the " + n * n + " elements of the first matrix----\n"); for(i = 0; i < n; i++) { for(j = 0; j < n; j++) { x[i][j] = in.nextInt(); a = x[i][j] * x[i][j]; sum1 += a; } } normal = (double) Math.sqrt(sum1); System.out.println ("\n\nThe normal of the given matrix is: " + normal); for(i = 0; i < n; i++) { sum = sum + x[i][i]; } System.out.println ("\nThe Trace of the given matrix is: " + sum + "\n"); } }
4 43 1 5 65 23 65 76 34 76 4 76 87 4 43 56 2
Output
Clear
ADVERTISEMENTS