#include<stdio.h>

int main()
{

int i,j,p,q,a[10][10],sum;

printf("\nEnter the order of the matrix \n\n");
scanf("%d",&p);

printf("\nEnter the %d elements of the matrix \n\n", p*p);

for(i = 0; i< p; i++)
{
for(j = 0; j<p ; j++)
{
scanf("%d", &a[i][j]);
}
}

printf("\nThe matrix is \n\n");
for(i = 0; i< p; i++)
{
for(j = 0; j<p ; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}

sum=0;

for(i=0; i<p ; i++)

{
sum= sum + a[i][i];

}

printf("\n\nThe Trace of matrix is: %d \n\n", sum);

return 0;
}
     
           
Note: Need to be arranged in compiler after copied
   

 OutPut:

Enter the order of the matrix 3 Enter the 9 elements of the matrix 1 2 3 4 5 6 7 8 9 The matrix is 1 2 3 4 5 6 7 8 9 The Trace of matrix is: 15