Monica - Treasure Key
Single File Programming Question
Problem Statement
Monica is interested in finding a treasure but the key to opening is to get the sum of the main diagonal elements and secondary diagonal elements.
Write a program to help Monica find the diagonal sum of a square 2D array.
Note: The main diagonal of the array consists of the elements traversing from the top-left corner to the bottom-right corner. The secondary diagonal includes elements from the top-right corner to the bottom-left corner.
Input:
format :The first line of input consists of an integer N, representing the number of rows and columns.
The following N lines consist of N space-separated integers, representing the 2D array elements.
Output:
format :The first line of output prints "Sum of the main diagonal: " followed by an integer, representing the sum of the main diagonal.
The second line prints "Sum of the secondary diagonal: " followed by an integer, representing the sum of the secondary diagonal.
Refer to the sample output for formatting specifications.
Code constraints : The input 2D array should be a square matrix.
Sample test cases :
Input 1:
3
1 2 3
4 5 6
7 8 9
Output 1 :
Sum of the main diagonal: 15
Sum of the secondary diagonal: 15
Input 2 :
3
1 2 5
3 6 7
8 5 3
Output 2 :
Sum of the main diagonal: 10
Sum of the secondary diagonal: 19