Write a program that take value of 3 x 3 matrix from user and display the sum of diagonal elements of a matrix .
PROGRAM : -
#include <iostream.h>
#include <conio.h>
main()
{
clrscr();
int arr[3][3],i,j,d1=0,d2;
cout<<"Please enter the values of 3x3 matrix :"<<endl;
for (i=0;i<3;i++)
for (j=0;j<3;j++)
cin>>arr[i][j];
cout<<"\nSo The Matrix Is : "<<"\n";
for (i=0;i<3;i++)
{
{
for (j=0;j<3;j++)
cout<<arr[i][j]<<" ";
}
cout<<"\n";
}
cout<<"\nSo The Sum Of Diagonals : "<<"\n";
for (i=0;i<3;i++)
for (j=0;j<3;j++)
{
if (i==j)
d1=d1+arr[i][j];
}
cout<<"Diagonal-1 = "<<d1<<"\n";
d2=arr[0][2]+arr[1][1]+arr[2][0];
cout<<"Diagonal-2 = "<<d2;
return 0;
}
OUTPUT : -
0 Comments