// C++ program to find the sum of diagonal elements without using pointers.
#include<iostream.h>#include<conio.h>
void main()
{
int a[10][10],p[10];
int m,n,i,j,s1=0,s2=0,k=0;
cout<<"Enter the order of matrics: ";
cin>>m>>n;
cout<<"Enter the elements\n\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"\n\nFirst diagonal elements are\n\n";
for(i=0;i<m;i++)
{
cout<<a[i][k]<<"\t";
s1=s1+a[i][k];
k++;
}
cout<<"\n\nSecond diagonal elements are\n\n";
k=m-1;
for(i=0;i<n;i++)
{
cout<<a[i][k]<<"\t";
s2=s2+a[i][k];
k--;
}
cout<<"\n\nSum of first diagonal elements is: ";
cout<<s1;
cout<<"\n\nSum of second diagonal elements is: ";
cout<<s2;
getch();
}
Output
C++ program to find the sum of diagonal elements of a matrix using pointers
No comments:
Post a Comment