Write a program to obtain a matrix and find the sum of its diagonal elements.Note: Only square matrix.Input format :The input consists of the number of rows and columns separated by a space.The second line of the input is matrix elements.Output format :The output prints the sum of diagonal elements.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :3 31 2 34 5 67 8 9Output 1 :15Input 2 :4 412 23 45 5678 89 98 8765 54 32 2114 25 36 58Output 2 :191
Question
Write a program to obtain a matrix and find the sum of its diagonal elements.Note: Only square matrix.Input format :The input consists of the number of rows and columns separated by a space.The second line of the input is matrix elements.Output format :The output prints the sum of diagonal elements.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :3 31 2 34 5 67 8 9Output 1 :15Input 2 :4 412 23 45 5678 89 98 8765 54 32 2114 25 36 58Output 2 :191
Solution
Here is a Python program that accomplishes the task:
# Function to calculate sum of diagonal elements
def diagonal_sum(matrix, n):
sum = 0
for i in range(0, n):
sum += matrix[i][i]
return sum
# Input number of rows and columns
n, m = map(int, input().split())
# Check if the matrix is square
if n != m:
print("Matrix is not square")
else:
# Input matrix elements
matrix = []
for i in range(n):
row = list(map(int, input().split()))
matrix.append(row)
# Print sum of diagonal elements
print(diagonal_sum(matrix, n))
This program first checks if the matrix is square (i.e., the number of rows equals the number of columns). If it is, it reads the matrix elements from the user, calculates the sum of the diagonal elements using the diagonal_sum function, and prints the result.
Similar Questions
Write a C Program to Compute the Sum of Diagonals of a square matrix. Print the sum of both diagonals separately. Get the number of rows and columns from the user.
Write a program to obtain a matrix and find the sum of the elements in the lower triangular matrix.Note: Only square matrixInput format :The first line of the input consists of the number of rows and columns.The second line of the input consists of the matrix element.Output format :The output prints the sum of the lower triangular matrix.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :3 312 23 4556 78 8995 51 20Output 1 :312
Write a C program that calculates the sum of both diagonals in a square matrix. Implement a function that takes a square matrix and its size as input and returns the sum of the main diagonal and anti-diagonal elements.Input3 31 2 3 4 5 6 7 8 9OutputSum of the main diagonal elements: 15Sum of the anti-diagonal elements: 15
Write a program to obtain a matrix and find the sum of each row and each column.Input format :The first line of the input consists of the value of the number of rows and the number of columns.The second line of the input consists of a matrix.Output format :The output prints the sum of each row and each column.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :4 41 2 3 45 6 7 89 10 11 1213 14 15 16Output 1 :Sum of the row 0 = 10Sum of the row 1 = 26Sum of the row 2 = 42Sum of the row 3 = 58Sum of the column 0 = 28Sum of the column 1 = 32Sum of the column 2 = 36Sum of the column 3 = 40Input 2 :3 398 87 6545 32 2845 56 58Output 2 :Sum of the row 0 = 250Sum of the row 1 = 105Sum of the row 2 = 159Sum of the column 0 = 188Sum of the column 1 = 175Sum of the column 2 = 151
For a given 2D square matrix of size N*N, the task is to find the sum of elements in the Principal and Secondary diagonals. For example, analyze the following 4 × 4 input matrix.a00 a01 a02 a03a10 a11 a12 a13a20 a21 a22 a23a30 a31 a32 a33Example:Input 1 : 6 7 3 4 8 9 2 1 1 2 9 6 6 5 7 2Output 1 : Principal Diagonal: 26 Secondary Diagonal: 14Intuition:1. The principal diagonal is constituted by the elements a00, a11, a22, a33, and the row-column condition for the principal diagonal is: row = column2. However, the secondary diagonal is constituted by the elements a03, a12, a21, a30, and the row-column condition for the Secondary diagonal is: row + column = N – 1
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.