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
Approach 1: Conditional Statements
C
// C Program to demonstrate the // Sum of Diagonals of a Matrix #include <stdio.h> int main()
{ int i, j, m = 3, n = 3, a = 0, sum = 0;
// input matrix
int matrix[3][3]
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// if both rows and columns are equal then it is
// possible to calculate diagonal sum
if (m == n) {
// printing the input matrix
printf ( "The matrix is \n" );
// iterates number of rows
for (i = 0; i < m; ++i) {
// iterates number of columns
for (j = 0; j < n; ++j) {
printf ( " %d" , matrix[i][j]);
}
printf ( "\n" );
}
for (i = 0; i < m; ++i) {
// calculating the main diagonal sum
sum = sum + matrix[i][i];
// calculating the off diagonal sum
a = a + matrix[i][m - i - 1];
}
// printing the result
printf ( "\nMain diagonal elements sum is = %d\n" , sum);
printf ( "Off-diagonal elements sum is = %d\n" , a);
}
else
// if both rows and columns are not equal then it is
// not possible to calculate the sum
printf ( "not a square matrix\n" );
return 0;
} |
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
Approach 2: Taking input matrix from the user
C
// C Program to Demonstrate the Sum of Diagonals // of a Matrix by taking input from the user #include <stdio.h> int main()
{ int i, j, m = 3, n = 3, a = 0, sum = 0;
int matrix[10][10];
// if both rows and columns are equal then it is
// possible to calculate diagonal sum
if (m == n) {
// entering the coefficients of the matrix
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
scanf ( "%d" , &matrix[i][j]);
}
}
// printing the input matrix
printf ( "The matrix is \n" );
// iterates number of rows
for (i = 0; i < m; ++i) {
// iterates number of columns
for (j = 0; j < n; ++j) {
printf ( " %d" , matrix[i][j]);
}
printf ( "\n" );
}
for (i = 0; i < m; ++i) {
// calculating the main diagonal sum
sum = sum + matrix[i][i];
// calculating the off diagonal sum
a = a + matrix[i][m - i - 1];
}
// printing the result
printf ( "\nMain diagonal elements sum is = %d\n" ,sum);
printf ( "Off-diagonal elements sum is = %d\n" , a);
}
else
// if both rows and columns are not equal then it is
// not possible to calculate the sum
printf ( "not a square matrix\n" );
return 0;
} |
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
Approach 3: Using functions
C
// C Program to demonstrate the Sum of Diagonals // of a Matrix by using functions #include <stdio.h> const int max = 10;
// Declaration of function int diagonal_sum( int m, int n, int matrix[][max])
{ int i, j, a = 0, sum = 0;
// if both rows and columns are equal then it is
// possible to calculate diagonal sum
if (m == n) {
// printing the input matrix
printf ( "The matrix is \n" );
// iterates number of rows
for (i = 0; i < m; ++i) {
// iterates number of columns
for (j = 0; j < n; ++j) {
printf ( " %d" , matrix[i][j]);
}
printf ( "\n" );
}
for (i = 0; i < m; ++i) {
// calculating the main diagonal sum
sum = sum + matrix[i][i];
// calculating the off diagonal sum
a = a + matrix[i][m - i - 1];
}
// printing the result
printf ( "\nMain diagonal elements sum is = %d\n" , sum);
printf ( "Off-diagonal elements sum is = %d\n" , a);
}
else
// if both rows and columns are not equal then it is
// not possible to calculate the sum
printf ( "not a square matrix\n" );
} int main()
{ int m = 3, n = 3;
// input matrix
int matrix[][10] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
diagonal_sum(m, n, matrix);
return 0;
} |
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