Here, we will compute the sum of diagonals of a Matrix using the following 3 methods:
- Using Conditional statements
- Taking Custom Input from the user whilst using Conditional Statements
- Using Functions
We will keep the same input in all the mentioned approaches and get an output accordingly.
Input:
The matrix is 1 2 3 4 5 6 7 8 9
Output:
Main diagonal elements sum is = 15 Off-diagonal elements sum is = 15
Explanation: The main diagonals are 1, 5, and 9. So, the sum of the main diagonals is 1+5+9=15. The off diagonals are 3, 5, and 7. So, the sum of the main diagonals is 3+5+7=15
Taking input matrix from the user
- C
Output:
The matrix is 1 2 3 4 5 6 7 8 9 Main diagonal elements sum is = 15 Off-diagonal elements sum is = 15
No comments:
Post a Comment