Write a program that input integer values in a 4x4 matrix and displays the sum of diagonal elements of the matrix.
Code:#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y;
int a[4][4],sum=0;
cout<<"Enter the elements of the matrix: "<<endl;
for(y=0; y<4; y++)
for(x=0; x<4; x++)
{
cout<<"Element"<<x+1<<" , "<<y+1<<" : ";
cin>>a[x][y];
}
for (x=0; x<4; x++)
for (y=0; y<4; y++)
if(x==y)
sum+=a[x][y];
cout<<"Sum of the diagonal elements is:"<<sum;
getch();
}
----------------------------------------------------------------------------------------------------------------
Post A Comment:
0 comments so far,add yours