1/* C++ program for Sum of diagonal elements of a matrix */
2#include<iostream>
3using namespace std;
4int main()
5{
6 int matrix[100][100], m, n ,i ,j, sum=0;
7 cout<<"Enter rows of matrix: ";
8 cin>>m;
9 cout<<"Enter columns of matrix: ";
10 cin>>n;
11
12 cout<<"Enter matrix elements: "<<endl;
13 for(i=0;i<m;i++)
14 {
15 for(j=0;j<n;j++)
16 {
17 cin>>matrix[i][j];
18 }
19 }
20
21 /* add diagonal elements into sum */
22 for(i=0;i<m;i++)
23 {
24 for(j=0;j<n;j++)
25 {
26 if(i==j)
27 sum=sum+matrix[i][j];
28 }
29 }
30
31 cout<<"Sum of diagonal elements: "<<sum;
32
33 return 0;
34}