C Program to Find Trace and Normal of a Matrix using For loop
ADVERTISEMENTS
C program to find trace and normal of a matrix using for loop. In this article, you will learn how to make program to find normal and trace of a matrix in c language using for loop.
What is the Normal of a square matrix?
The square root of the sum of the squares of each element of the matrix.
What is the Trace of a square matrix?
The sum of the diagonal elements of the square matrix.
Source Code
// C Program to Find Trace and Normal of a Matrix using For loop
#include <stdio.h>
#include <math.h>
int main() {
int i, j, n, sum = 0, sum1 = 0, a = 0, normal;
printf("\nEnter the number of rows[columns] of the matrix::\n\n");
scanf("%d", &n);
int x[n][n];
printf("\n----Enter the %d elements of the first matrix----\n\n", n * n);
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &x[i][j]);
a = x[i][j]*x[i][j];
sum1 += a;
}
}
normal = sqrt((double)sum1);
printf("\n\nThe normal of the given matrix is: %d", normal);
for(i = 0; i < n; i++) {
sum = sum + x[i][i];
}
printf("\n\nThe Trace of the given matrix is: %d\n", sum);
return 0;
}
Output
Enter the number of rows[columns] of the matrix::
4
----Enter the 16 elements of the first matrix----
12 32 34 12 45 65 77 67 32 89 23 45 34 12 65 23
The normal of the given matrix is: 191
The Trace of the given matrix is: 123