C program to find sum of upper triangular matrix

Write a C program to read elements in a matrix and find sum of upper triangular matrix. How to find sum of upper triangular matrix in C. Logic to find sum of upper triangular matrix.

Required knowledge

Basic C programming, C for loop, ArrayS

Upper triangular matrix

Upper triangular matrix is a special square matrix whose all elements below main diagonal is zero.

Upper triangular matrix

Logic to find sum of upper triangular matrix

Sum of upper triangular matrix is denoted with below image. In below image we need to find sum of matrix elements inside the red triangle.

Upper triangular matrix elements

Formula to find sum of all elements matrix A inside the red triangular area is given by -

sum = sum + Aij (Where i<j).

Program to find sum of upper triangular matrix



 
/**
 * C program to find sum of upper triangular matrix
 */

#include <stdio.h>
#define MAX_ROWS 3
#define MAX_COLS 3

int main()
{
    int A[MAX_ROWS][MAX_ROWS];
    int row, col, sum = 0;
    
    /* Input elements in matrix from user */
    printf("Enter elements in matrix of size %dx%d: \n", MAX_ROWS, MAX_COLS);
    for(row=0; row<MAX_ROWS; row++)
    {
        for(col=0; col<MAX_COLS; col++)
        {
            scanf("%d", &A[row][col]);
        }
    }
    
    /* Find sum of upper triangular matrix */
    for(row=0; row<MAX_ROWS; row++)
    {
        for(col=0; col<MAX_COLS; col++)
        {
            if(col>row)
            {
                sum += A[row][col];
            }
        }
    }

    printf("Sum of upper triangular matrix = %d", sum);

    return 0;
}



Output:

Enter elements in matrix of size 3x3:
10 20 30
0  50 60
0   0 90
Sum of upper triangular matrix = 110
Process returned 0 (0x0) execution time : 38.057 s
Enter elements in matrix of size 3x3:
1 2 3
4 5 6
7 8 9
Sum of upper triangular matrix = 11
Process returned 0 (0x0) execution time : 14.943 s

Program to find sum of upper triangular matrix




/**
 * C program to find sum of upper triangular matrix
 */

#include <stdio.h>

int main()
{
    int A[10][10];
    int row, col, sum = 0,m,n;
    printf("Enter number of rows:");
    scanf("%d",&m);
    printf("Enter number of columns:");
    scanf("%d",&n);

    /* Input elements in matrix from user */
    printf("Enter elements in matrix of size %dx%d: \n", m, n);
    for(row=0; row<m; row++)
    {
        for(col=0; col<n; col++)
        {
            scanf("%d", &A[row][col]);
        }
    }

    /* Find sum of upper triangular matrix */
    for(row=0; row<m; row++)
    {
        for(col=0; col<n; col++)
        {
            if(col>row)
            {
                sum += A[row][col];
            }
        }
    }

    printf("Sum of upper triangular matrix = %d", sum);

    return 0;
}



Output:

Enter number of rows:3
Enter number of columns:3
Enter elements in matrix of size 3x3:
9 8 7
0 5 4
0 0 1
Sum of upper triangular matrix = 19
Process returned 0 (0x0) execution time : 26.826 s



Instagram