fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main() {
  5. int n, i, j;
  6. int trace = 0;
  7. double norm = 0.0;
  8.  
  9. printf("Enter the size of the square matrix: ");
  10. scanf("%d", &n);
  11.  
  12. int matrix[n][n];
  13.  
  14. printf("Enter the elements of the matrix:\n");
  15. for (i = 0; i < n; i++) {
  16. for (j = 0; j < n; j++) {
  17. scanf("%d", &matrix[i][j]);
  18. }
  19. }
  20.  
  21. // Calculate trace and norm
  22. for (i = 0; i < n; i++) {
  23. trace += matrix[i][i]; // sum of principal diagonal elements
  24. for (j = 0; j < n; j++) {
  25. norm += matrix[i][j] * matrix[i][j]; // sum of squares of elements
  26. }
  27. }
  28.  
  29. norm = sqrt(norm); // Square root of sum of squares
  30.  
  31. printf("Trace of the matrix: %d\n", trace);
  32. printf("Norm of the matrix: %.2f\n", norm);
  33.  
  34. return 0;
  35. }
  36.  
  37.  
Success #stdin #stdout 1.25s 5292KB
stdin
Standard input is empty
stdout
Enter the size of the square matrix: Enter the elements of the matrix:
Trace of the matrix: 0
Norm of the matrix: 231216.49