Find the sum of upper & lower traiangular elements
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int mat[10][10];
int i, j, size, upper = 0, lower = 0;
clrscr();
printf("Enter size of the square matrix : ");
scanf("%d", &size);
printf("\nEnter the elements of the matrix : \n\n");
for(i = 0; i < size; i++)
for(j = 0; j < size; j++)
scanf("%d", &mat[i][j]);
printf("\nThe upper triangular matrix is : \n\n");
for(i = 0; i < size; i++)
{
for(j = 0; j < size; j++)
{
if(i <= j)
{
printf("%d\t", mat[i][j]);
upper = upper + mat[i][j];
}
else
printf("\t");
}
printf("\n");
}
printf("\nThe lower triangular matrix is : \n\n");
for(i = 0; i < size; i++)
{
for(j = 0; j < size; j++)
{
if(j <= i)
{
printf("%d\t", mat[i][j]);
lower = lower + mat[i][j];
}
else
printf("\t");
}
printf("\n");
}
printf("\nThe sum of upper triangular elements is : %d\n",upper);
printf("\nThe sum of lower triangular elements is : %d", lower);
getch();
}
Output:
Enter size of the square matrix : 3
Enter the elements of the matrix :
1 2 3
4 5 6
7 8 9
The upper triangular matrix is :
1 2 3
5 6
9
The lower triangular matrix is :
1
4 5
7 8 9
The sum of upper triangular elements is : 26
The sum of lower triangular elements is : 34
-
UpdatedDec 31, 2019
-
Views16,179
You May Like
Program to maintain employee details using structures
Program to maintain student details using structures
Check whether the person is eligible to vote or not
Print the numbers that are divisible by a given no.
Program to generate magic square
To sort the given numbers in ascending & descending order