Skip to content

Commit eb38541

Browse files
Merge pull request #102 from MonalikaKapoor/Lower-and-Upper-Half
Added code to print Lower and upper half of a triangle matrix
2 parents 3a2c6c7 + afd9d71 commit eb38541

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// C++ Program to Print Lowerhalf and Upperhalf of Triangle Matrix
2+
3+
4+
#include<iostream>
5+
6+
using namespace std;
7+
8+
int main()
9+
{
10+
int a[10][10],i,j,m;
11+
cout<<"Enter size of the Matrix(min:3,max:5):";
12+
cin>>m;
13+
cout<<"\nEnter the Matrix row wise:\n";
14+
15+
for(i=0;i<m;i++)
16+
for(j=0;j<m;++j)
17+
cin>>a[i][j];
18+
19+
cout<<"\n\n";
20+
21+
cout<<"Upperhalf of Triangle Matrix :: \n";
22+
23+
for(i=0;i<m;++i)
24+
{
25+
for(j=0;j<m;++j)
26+
{
27+
if(i<j)
28+
cout<<a[i][j]<<" ";
29+
else
30+
cout<<" ";
31+
}
32+
33+
cout<<"\n";
34+
}
35+
36+
cout<<"\n";
37+
38+
cout<<"Lowerhalf of Triangle Matrix :: \n";
39+
40+
for(i=0;i<m;++i)
41+
{
42+
for(j=0;j<m;++j)
43+
{
44+
if(j<i)
45+
cout<<a[i][j]<<" ";
46+
else
47+
cout<<" ";
48+
}
49+
cout<<"\n";
50+
}
51+
52+
return 0;
53+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* C++ Program to Find Sum of Diagonals elements in a Matrix */
2+
3+
#include<iostream>
4+
5+
using namespace std;
6+
7+
int main()
8+
{
9+
int a[10][10],d1sum=0,d2sum=0,m,i,j;
10+
cout<<"Enter size of matrix :: ";
11+
cin>>m;
12+
cout<<"\nEnter Elements to Matrix Below :: \n";
13+
14+
for(i=0;i<m;i++)
15+
{
16+
for(j=0;j<m;++j)
17+
{
18+
cout<<"\nEnter a["<<i<<"]["<<j<<"] Element :: ";
19+
cin>>a[i][j];
20+
}
21+
22+
}
23+
24+
cout<<"\nThe given matrix is :: \n\n";
25+
for (i = 0; i < m; ++i)
26+
{
27+
for (j = 0; j < m; ++j)
28+
{
29+
cout<<"\t"<<a[i][j];
30+
}
31+
printf("\n\n");
32+
}
33+
34+
35+
36+
for(i=0;i<m;++i)
37+
for(j=0;j<m;++j)
38+
{
39+
if(i==j)
40+
d1sum+=a[i][j];
41+
if(i+j==(m-1))
42+
d2sum+=a[i][j];
43+
}
44+
45+
cout<<"\nSum of 1st diagonal is :: "<<d1sum;
46+
cout<<"\n\nSum of 2nd diagonal is :: "<<d2sum;
47+
48+
return 0;
49+
}

0 commit comments

Comments
 (0)