Sunday, August 4, 2013

Program to create a square matrix and find the sum of diagonal elements

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[20][20],sum=0,m,n,i,j;
cout<<"Enter the size : ";
cin>>m>>n;
if(m==n)
{
cout<<"Enter the elements : ";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(i==j)
sum+=a[i][j];
}
}
cout<<sum;
}
else
cout<<"Not a square matrix";
getch();
}

No comments:

Post a Comment