# C Program to Find Normal and Trace of a Matrix

This is a program to find the trace & normal of a given matrix.

Problem Description

This C Program find the trace & normal of a given matrix. Here trace of the matrix is the sum of the elements of the main diagonal i.e the diagonal from the upper left to the lower right of a matrix. Normal of the matrix is the square root of the sum of all the elements.

Problem Solution

1. Create a matrix and define all its elements.
2. To evaluate normal of the matrix, take sum of all the elements of the array and calculate the square root of it.
3. To evaluate trace of the matrix, take sum of the main diagonal elements.

Program/Source Code

Here is source code of the C program to find the trace & normal of a given matrix. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

1. `    /*`
2. `     * C program to find the trace and normal of a matrix`
3. `     *`
4. `     * Trace is defined as the sum of main diagonal elements and`
5. `     * Normal is defined as square root of the sum of all the elements`
6. `     */`
7. ` `
8. `    #include <stdio.h>`
9. `    #include <math.h>`
10. ` `
11. `    void main ()`
12. `    {`
13. ` `
14. `        static int array[10][10];`
15. `        int i, j, m, n, sum = 0, sum1 = 0, a = 0, normal;`
16. ` `
17. `        printf("Enter the order of the matrix\n");`
18. `        scanf("%d %d", &m, &n);`
19. ` `
20. `        printf("Enter the n coefficients of the matrix \n");`
21. `        for (i = 0; i < m; ++i) `
22. `        {`
23. `            for (j = 0; j < n; ++j)`
24. `            {`
25. `                scanf("%d", &array[i][j]);`
26. `                a = array[i][j] * array[i][j];`
27. `                sum1 = sum1 + a;`
28. `            }`
29. `        }`
30. ` `
31. `        normal = sqrt(sum1);`
32. `        printf("The normal of the given matrix is = %d\n", normal);`
33. `        for (i = 0; i < m; ++i) `
34. `        {`
35. `            sum = sum + array[i][i];`
36. `        }`
37. ` `
38. `        printf("Trace of the matrix is = %d\n", sum);`
39. ` `
40. `    }`
Program Explanation

1. Declare a matrix and define all its elements.
2. Declare variables for storing the normal and trace of the matrix.
3. Find the sum of all the elements of the matrix using nested for loop.
4. Evaluate normal by passing the above calculated sum to sqrt() function.
5. Take sum of all the main diagonal elements of the array to calculate trace.
6. Print trace and normal.

Runtime Test Cases
```Enter the order of the matrix
3 3
Enter the coefficients of the matrix
3  7 9
2 6 10
8 5 9
The normal of the given matrix is = 21
Trace of the matrix is = 18```

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]