PROGRAM TO CALCULATE SUM OF DIAGONAL ELEMENTS OF A MATRIX

/*PROGRAM TO CALCULATE SUM OF DIAGONAL ELEMENTS OF A MATRIX*/

#include<conio.h>

#include<iostream.h>

class sum_elements

{

int i,j,m,n,a[5][5],sum;

public:

void input();

void output();

};

void sum_elements::input()

{

cout<<“Enter the order of the matrix: “;

cin>>m>>n;

}

void sum_elements::output()

{

if(m==n)

{

cout<<“Enter “<<m*n<<” elements of the matrix: \n”;

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

cin>>a[i][j];

}

sum=0;

cout<<“Elements of the matrix are: \n”;

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

cout<<a[i][j]<<”   “;

if(i==j)

sum=sum+a[i][j];

}

cout<<endl;

}

cout<<“Sum of diagonal elements of the matrix is: “<<sum;

}

else

cout<<“Matrix must be a square matrix”;

}

void main()

{

sum_elements s1;

clrscr();

s1.input();

s1.output();

getch();

}

Leave a Reply

Your email address will not be published. Required fields are marked *