Home

A program to find trace of a matrix

C Programming Language 058

The following code shows how to write a program to find trace of a matrix.

Program

#include <stdio.h>

void main()
{
    int a[10][10], m, n, i, j, tr = 0;
    printf("Enter order m,n [m x n]: ");
    scanf("%d %d", &m, &n);
    
    printf("Enter values:\n");
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            scanf("%d", &a[i][j]);
            if (i == j)
            {
                tr = tr + a[i][j];
            }
        }
    }

    printf("Trace = %d", tr);

    printf("\n");
}

Result

Enter order m,n [m x n]: 2 2
Enter values:
1 2
3 4
Trace = 5


Last Updated on

Next Post: GraphProtocol: TS2322 null assignment in Subgraph →

Comments