Program to find diagonal of matrix | C++ Programming

Program to find diagonal of matrix | C++ Programming
//Program to find diagonal of matrix
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int A[10][10],B[10][10],m,n,i,j;
cout<<"\n enter the no. of rows:";
cin>>m;
cout<<"\n enter no. of column:";
cin>>n;
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
 { cout<<"/n enter A["<<i<<"]["<<j<<"] element:";
 cin>>A[i][j];
 }
  }
   cout<<"/n the matrix is:";
   for(i=0;i<m;i++)
   { cout<<"\n";
  for(j=0;j<n;j++)
    cout<<A[i][j]<<" ";
    }
  cout<<"/n first diagonal is:\n";
  for(i=0;i<n;i++)
  { for(j=0;j<m;j++)
  { if(i==j)
 cout<<A[i][j]<<" ";
 }
  }
   cout<<"\n second diagonal is:";
   for(i=0;i<n;i++)
   { for(j=0;j<m;j++)
  { if(i+j==n-1)
  cout<<A[i][j]<<" ";
  }
   }
    int sum1=0,sum2=0;
    for(i=0;i<n;i++)
    { for(j=0;j<m;j++)
    { if(i==j)
      sum1+=A[i][j];
      if(i+j==n-i)
sum2+=A[i][j];
}
}
cout<<"\n sum of first diagonal element="<<sum1;
cout<<"\n sum of second diagonal element="<<sum2;
  if(i==j)
  { cout<<A[i][j]<<" ";
  sum1+=A[i][j];
}
getch();
}
Program to find diagonal of matrix | C++ Programming Program to find diagonal of matrix | C++ Programming Reviewed by SHERE KHOJRAHA on February 13, 2019 Rating: 5

No comments:

Powered by Blogger.