Write a program to print the quotient and remainder on dividing sum of left-top to right-bottom diagonal by sum of right-top to left-bottom diagonal.
Question
Write a program to print the quotient and remainder on dividing sum of left-top to right-bottom diagonal by sum of right-top to left-bottom diagonal.
Solution
Sure, here is a Python program that does that:
def diagonal_sum(matrix):
# Initialize sums of diagonals
d1 = d2 = 0
n = len(matrix)
for i in range(0, n):
for j in range(0, n):
# finding sum of primary diagonal
if (i == j):
d1 += matrix[i][j]
# finding sum of secondary diagonal
if (i == n - j - 1):
d2 += matrix[i][j]
# printing the result
if d2 != 0:
print("Quotient is", d1 // d2)
print("Remainder is", d1 % d2)
else:
print("Cannot divide by zero")
# Test the function
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
diagonal_sum(matrix)
This program first calculates the sum of the elements in the left-top to right-bottom diagonal (primary diagonal) and the right-top to left-bottom diagonal (secondary diagonal) of the matrix. Then it prints the quotient and remainder of the division of the sum of the primary diagonal by the sum of the secondary diagonal. If the sum of the secondary diagonal is zero, it prints a message saying that division by zero is not possible.
Similar Questions
Complete the code fragment to read two integer inputs from keyboard and find the quotient and remainder.
C Program to Calculate Multiplication and Addition, and Check Division Remainder
Output FormatReturn the absolute difference between the sums of the matrix's two diagonals as a single integer.
The output of the code will be ____. int i=5; printf(“%d %d %d”, i, i++, ++i); ans. 5 5 7 7 6 6 7 6 5 7 6 7
Write a program to enter two numbers and print the arithmeticoperations like +,-,*,**, /, // and % using function
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.