In this program user ask to make sum of upper and lower triangle of matrix. In this program user declare the array type Variable after declaring value to the variable if statement will be use to check whether the matrix is square or not. Else it will be going to enter elements for matrix. Loop statement will use to calculate the procedure for elements. For loop and control statements will use the procedure to make it. Then a print method will use to display the sum of upper triangle.
Next move is to calculate the sum of lower matrix triangle as per procedure. Here the same logic use to calculate the sum of lower matrix triangle along with using loop statement and control statement and to get the result in the end.
Problem Statement:-
This program is an implementation about to calculate the sum of lower and upper triangle matrix.
- Enter the matrix elements.
- Checks the mode of matrix.
- Display the result on screen.
This C program is successfully compiled and run on a System. Output is shown below.
#include<stdio.h>
void main()
{
int a[10][10],sum=0,sm=0;
int i,j,m,n;
clrscr();
printf("Enter Order of Matrix : ");
scanf("%d%d",&m,&n);
if(m!=n)
{
printf("Not a Square Matrix : ");
getch();
exit();
}
printf("Enter Elements : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i+j<m-1)
{
sum+=a[i][j];
}
}
}
printf("\n Sum of Upper Triangle is : %d",sum);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i+j>m-1)
{
sm+=a[i][j];
}
}
}
printf("\n Sum of Lower Triangle is : %d",sm);
getch();
}