n to the length of the matrix (number of rows/columns).ans to 0 to store the sum of the diagonal elements.i = 0 to i = n - 1:
mat[i][i] to ans (primary diagonal).mat[i][n - 1 - i] to ans (secondary diagonal).n is odd, subtract the central element mat[n / 2][n / 2] from ans.ans.