using numpy arange() function and then calculate the principal diagonal (the diagonal from the upper

left to the lower right) elements sum .again calculate the secondary diagonal(the diagonal from the

upper right to the lower left) elements sum.

__Example:__ [[ 0 1 2]

[ 3 4 5]

[ 6 7 8 ]]

** **__Code:__

# create a class

class diagnoal_sum:

# create function

def sum_diagonal( self,a, r,c):

# check wheather a given a row and column is valid or not.

if r!= c or c==0 or r==0:

return print("Enter valid matrix ,Matrix should be (n x n) format")

# calculate the size of matrix

l=len(a[0])

# if size is null then return matrix

# initialize variable with 0 value

main=0

second=0

for i in range(r):

#count matrix diagonal element which is principal element of matrix a[n][n]

main +=a[i][i]

#count matrix diagonal element which is secondary element of matrix a[n][l-i-1] .example:

# a[2][0],a[1][1] a[0][2] f matrix is 3 x 3.

second+=a[i][l-i-1]

print("main diagonal elements sum :", main)

print("second diagonal elements sum :", second)

# define main() function.

def main():

digsum=diagnoal_sum()

#taking input from console.

row,col=input("enter row and column :").split()

r=int(row)

c=int(col)

# create matrix using numpy function arange() in sequential order.

a=np.arange(r*c).reshape(r,c)

print("given matrix :\n", a)

#call function to execute .

digsum.sum_diagonal(a,r,c)

if __name__ == "__main__":

main()

**Output: **

[[ 0 1 2 3 4 5]

[ 6 7 8 9 10 11]

[12 13 14 15 16 17]]

Enter valid matrix ,Matrix should be (n x n) format

**Time complexity is O(n) ans space complexity is O(1)** **keep coding happy coding :)**

[ 3 4 5]

[ 6 7 8 ]]

principal diagonal elements sum : 0+4+8= 12

principal diagonal elements sum : 2+4+6= 12

#import numpy library

import numpy as np# create a class

class diagnoal_sum:

# create function

def sum_diagonal( self,a, r,c):

# check wheather a given a row and column is valid or not.

if r!= c or c==0 or r==0:

return print("Enter valid matrix ,Matrix should be (n x n) format")

# calculate the size of matrix

l=len(a[0])

# if size is null then return matrix

# initialize variable with 0 value

main=0

second=0

for i in range(r):

#count matrix diagonal element which is principal element of matrix a[n][n]

main +=a[i][i]

#count matrix diagonal element which is secondary element of matrix a[n][l-i-1] .example:

# a[2][0],a[1][1] a[0][2] f matrix is 3 x 3.

second+=a[i][l-i-1]

print("main diagonal elements sum :", main)

print("second diagonal elements sum :", second)

# define main() function.

def main():

digsum=diagnoal_sum()

#taking input from console.

row,col=input("enter row and column :").split()

r=int(row)

c=int(col)

# create matrix using numpy function arange() in sequential order.

a=np.arange(r*c).reshape(r,c)

print("given matrix :\n", a)

#call function to execute .

digsum.sum_diagonal(a,r,c)

if __name__ == "__main__":

main()

case1: enter row and column :2 2

[[0 1]

[2 3]]

main diagonal elements sum : 3

second diagonal elements sum : 3

[[0 1]

[2 3]]

main diagonal elements sum : 3

second diagonal elements sum : 3

case2:

enter row and column :0 2

given matrix : []

Enter valid matrix, Matrix should be (n x n) format

case3:

enter row and column :3 6

given matrix :[[ 0 1 2 3 4 5]

[ 6 7 8 9 10 11]

[12 13 14 15 16 17]]

Enter valid matrix ,Matrix should be (n x n) format

case 4:

enter row and column : 0 0

given matrix :

[]

Enter valid matrix ,Matrix should be (n x n) format

## Comments