0% found this document useful (0 votes)
160 views3 pages

Date: 24.12.2021 Program No:16 Matrix Operation Using Nested List Problem Definition: Aim: Program

The document presents a Python program that takes user input to create a nested list of size MxN. It then defines functions to calculate the sum of the left diagonal, right diagonal, boundary elements, and non-boundary elements of the nested list. The program displays an interactive menu to call each function and output the corresponding sum.

Uploaded by

Muthumeenatchi U
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views3 pages

Date: 24.12.2021 Program No:16 Matrix Operation Using Nested List Problem Definition: Aim: Program

The document presents a Python program that takes user input to create a nested list of size MxN. It then defines functions to calculate the sum of the left diagonal, right diagonal, boundary elements, and non-boundary elements of the nested list. The program displays an interactive menu to call each function and output the corresponding sum.

Uploaded by

Muthumeenatchi U
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Date : 24.12.

2021 Program No :16


MATRIX OPERATION USING NESTED LIST
Problem Definition:
Write an interactive Python program to find the sum of boundary, non-
boundary, left diagonal and right diagonal elements of a nested List of size MxN
AIM:
To write an interactive Python program to find the sum of boundary, non-
boundary, left diagonal and right diagonal elements of a nested List of size MxN
Program:
row=int(input("Enter the row size for nested list"))
column=int(input("Enter the column size for nested list"))
arr=[]
for i in range(row):
ilist=[]
print(i+1,"Row Element")
for j in range(column):
item=int(input("Enter the element"))
ilist.append(item)
arr.append(ilist)
print()
print(arr)

def leftdiagonal():
sum=0
for i in range(row):
for j in range(column):
if i==j:
sum=sum+arr[i][j]
print("Sum of left diagonal=",sum)

def rightdiagonal():
sum=0
for i in range(row):
for j in range(column):
if i+j== row-1:
sum=sum+arr[i][j]
print("sum of right diagonal=",sum)

def sumofboundary():
sum=0
for i in range(row):
for j in range(column):
if i==0 or i==row-1 or j==0 or j==column-1:
sum=sum+arr[i][j]
print("Sum of boundary=",sum)

def sumofnonboundary():
sum=0
for i in range(row):
for j in range(column):
if i!=0 and i!=row-1 and j!=0 and j!=column-1:
sum=sum+arr[i][j]
print("Sum of boundary=",sum)
ch='y'
while ch=='y':
print("1. sum of left diagonal")
print("2. sum of right diagonal")
print("3. sum of boundary")
print("4. sum of non boundary")
ch=int(input("Enter your choice"))
if ch==1:
leftdiagonal()
elif ch==2:
rightdiagonal()
elif ch==3:
sumofboundary()
elif ch==4:
sumofnonboundary()
else:
print("Wrong choice")
print("Do you want to continue")
ch=input("enter your choice y / n")
OUTPUT

Enter the row size for nested list3


Enter the column size for nested list3
1 Row Element
Enter the element1
Enter the element2
Enter the element3

2 Row Element
Enter the element4
Enter the element5
Enter the element6

3 Row Element
Enter the element7
Enter the element8
Enter the element9

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]


1. sum of left diagonal
2. sum of right diagonal
3. sum of boundary
4. sum of non boundary
Enter your choice1
Sum of left diagonal= 15
Do you want to continue
enter your choice y / ny
1. sum of left diagonal
2. sum of right diagonal
3. sum of boundary
4. sum of non boundary
Enter your choice2
sum of right diagonal= 15
Do you want to continue
enter your choice y / ny
1. sum of left diagonal
2. sum of right diagonal
3. sum of boundary
4. sum of non boundary
Enter your choice3
Sum of boundary= 40
Do you want to continue
enter your choice y / ny
1. sum of left diagonal
2. sum of right diagonal
3. sum of boundary
4. sum of non boundary
Enter your choice4
Sum of boundary= 5
Do you want to continue
enter your choice y / nn

You might also like