Sum of Diagonal Elements of Matrix Using C++ Language

Program code;

#include<iostream>
using namespace std;

int main()
{
    int  a[2][2], sum=0;
cout<<"***************************************"<<endl;
cout<<"Sum of Diagonal Elements of Matrix"<<endl;
cout<<"***************************************"<<endl<<endl;
cout<<"Enter Elements of 2*2 Matrix A :"<<endl;
for(int i=0;i<2;i++)
{
for(int j = 0;j<2;j++)
{
cin>>a[i][j];
}

}

cout<<endl<<"Matrix A =  "<<endl;
for(int i=0;i<2;i++)
{
for(int j = 0;j<2;j++)
{
cout<<" \t\t "<<a[i][j]<<"  ";
}
cout<<endl;
}

for(int i=0;i<2;i++)
{
for(int j = 0;j<2;j++)
{
if(i==j)
sum = sum + a[i][j];
}
}
cout<<endl<<endl<<"Sum of Diagonal elements of matrix A is  =  "<<sum;

return 0;

}

Output:


No comments:

Post a Comment

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.