```
def get_sum_of_diagonals(mat):
sd1 = 0
sd2 = 0
for i in range(0, len(mat)):
for j in range(0, len(mat)):
if (i == j):
sd1 += mat[i][j]
if ((i + j) == (len(mat)-1)):
sd2 += mat[i][j]
return sd1+sd2
mat = [[1,2,9],[4,5,6],[18,8,12]]
print(get_sum_of_diagonals(mat))
```

# Sum of the Diagonals

Carvia Tech | June 21, 2019 | 1 min read | 5 views

Given a square matrix of size N by N, calculate the sums of its diagonals. There are two diagonals in each array.

Output

50

###### Top articles in this category:

- Python coding challenges for interviews
- Google Data Scientist interview questions with answers
- Top 100 interview questions on Data Science & Machine Learning
- Python Flask Interview Questions
- Sequence of Differences in Python
- Pass the ball game: NxN matrix in python
- Derivative of 1/x & Tossing a coin problem

###### Find more on this topic:

##### Machine Learning

Data science, machine learning, python, R, big data, spark, the Jupyter notebook, and much more

Last updated 1 week ago

###### Recommended books for interview preparation:

You may also be interested in..

You may also be interested in..

You may also be interested in..

You may also be interested in..

##### Similar Posts

- Installing PySpark with Jupyter notebook on Ubuntu 18.04 LTS
- Send email with attachment in Python
- Send rich text multimedia email in Python
- Blueprints in Flask API Development
- Singleton Design Pattern in Python
- SVM after LSTM deep learning model for text classification
- RuntimeError: get_session is not available when using TensorFlow 2.0
- Deploying Keras Model in Production with TensorFlow 2.0
- Python coding challenges for interviews
- Python Flask Interview Questions

Free Email Updates

Enter your email address to subscribe to this blog and receive notifications of new posts by email.