#C9689. Sum of Diagonals in a Square Matrix
Sum of Diagonals in a Square Matrix
Sum of Diagonals in a Square Matrix
Given a square matrix of size \(n \times n\) filled with integers, your task is to compute the sum of the primary diagonal and the secondary diagonal. Note that if \(n\) is odd, the central element (which lies on both diagonals) is counted twice; therefore, subtract it once to obtain the correct total.
For example, consider the matrix below:
\[ \begin{pmatrix} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end{pmatrix} \]The sum of the diagonals is \(1+5+9 + 3+5+7 - 5 = 25\).
inputFormat
The first line contains an integer (n) representing the size of the square matrix. Each of the next (n) lines contains (n) space-separated integers which represent the elements of the matrix.
outputFormat
Output a single integer: the sum of the primary and secondary diagonals of the matrix. If the matrix size is odd, subtract the middle element once to eliminate its double counting.## sample
3
1 2 3
4 5 6
7 8 9
25
</p>