Signup/Sign In

C Program To Sum of Upper Triangular Matrix

A matrix in which all the elements under the main diagonal are zero is known as an upper triangular matrix. Here, we are given a matrix and we have to calculate the sum of all the elements in the upper triangular matrix. But before moving forward if you are not familiar with the concept of the array in C, then do check the article on Arrays in C.

Below is the pictorial representation of the upper triangular matrix.

Input: Enter the Matrix:

1 2 3

4 5 6

7 8 9

Output: 26

Explanation: Here, the upper triangle elements are 1 2 3 5 6 9. These elements add up to a sum of 26.

Program 1: Sum of Upper Triangular Matrix

In this method, firstly we will declare a 2d array and then initialize it. After that, the sum of all the elements in the upper triangular matrix is calculated directly.

Algorithm

  1. Start
  2. Declare an M*N 2D array.
  3. Initialize the array.
  4. If the number of rows and columns are not equal then it is not possible to display the sum of the elements of the upper triangle.
  5. If the number of rows is equal to the number of columns, then use two loops to iterate through the loops.
  6. If (i<=j), then add all the elements.
  7. Display the final result.
  8. Stop

In the below program, we have to find the sum of all the elements in the upper triangular matrix directly.

#include <stdio.h>

int main()
{
    int m,n;                 //Matrix Size Declaration
    printf("Enter the number of rows and column: \n");
    scanf("%d %d",&m,&n);   //Matrix Size Initialization
    int arr[10][10];        //Matrix Size Declaration
    printf("\nEnter the elements of the matrix: \n");
    for(int i=0;i<m;i++)    //Matrix Initialization
    {
        for(int j=0;j<n;j++)
        {
            scanf("%d",&arr[i][j]);
        }
    }
    printf("\nThe elements in the matrix are: \n");
    for(int i=0;i<m;i++)     //Print the matrix
    {
        for(int j=0;j<n;j++)
        {
            printf("%d ",arr[i][j]);
        }
        printf("\n");
    }
    if(m==n)               //If number of rows and columns equal
    {
       int upsum=0;
       for(int i=0;i<m;i++)
       {
          for(int j=0;j<n;j++)
          {
               if(i<=j)          //Traverse only in the upper triangle
               upsum=upsum+arr[i][j];    //Add the elements 
          }
        }
        //Print the sum of upper triangular elements
        printf("\nThe sum of upper triangular matrix is %d",upsum);
    }
    else
    {                   //Not possible to declare upper triangular elements 
        printf("Not Possible to display lower triangular elements sum");
    }
    return 0;
}


Enter the number of rows and column: 3 3

Enter the elements of the matrix: 1 2 3 4 5 6 7 8 9

The elements in the matrix are:
1 2 3
4 5 6
7 8 9

The sum of upper triangular matrix is 26

Program 2: Sum of Upper Triangular Matrix

In this method, we use a function to display the sum of the upper triangle elements in the matrix.

Algorithm

  1. Start
  2. Declare an M*N 2D array.
  3. Initialize the array.
  4. If the number of rows and columns are not equal then it is not possible to display the sum of the elements of the upper triangle.
  5. If the number of rows is equal to the number of columns, then use two loops to iterate through the loops.
  6. Now call a function that will display the sum of upper triangle elements in the matrix.
  7. Using two for loops traverse through the elements.
  8. If (i<=j), then add all the elements.
  9. Display the final result.
  10. Stop.

In this program, the sum of the upper triangular matrix is calculated by using functions.

Here, after declaring and initializing the matrix a function is called which will add all the elements in the upper triangle.

#include <stdio.h>
void usum(int arr[10][10], int m, int n);     //Function Declaration

int main()
{
    int m,n;                 //Matrix Size Declaration
    printf("Enter the number of rows and column: \n");
    scanf("%d %d",&m,&n);   //Matrix Size Initialization
    int arr[10][10];        //Matrix Size Declaration
    printf("\nEnter the elements of the matrix: \n");
    for(int i=0;i<m;i++)    //Matrix Initialization
    {
        for(int j=0;j<n;j++)
        {
            scanf("%d",&arr[i][j]);
        }
    }
    printf("\nThe elements in the matrix are: \n");
    for(int i=0;i<m;i++)     //Print the matrix
    {
        for(int j=0;j<n;j++)
        {
            printf("%d ",arr[i][j]);
        }
        printf("\n");
    }
    if(m==n)               //If number of rows and columns equal
    {
       usum(arr,m,n);      //Call the function
    }
    else
    {                   //Not possible to declare upper triangular elements 
        printf("Not Possible to display lower triangular elements sum");
    }
    return 0;
}
void usum(int arr[10][10], int m, int n)    //Function Definition
{
    int upsum=0;
    for(int i=0;i<m;i++)
       {
          for(int j=0;j<n;j++)
          {
               if(i<=j)          //Traverse only in the upper triangle
               upsum=upsum+arr[i][j];    //Add the elements 
          }
        }
        //Print the sum of upper triangular elements
        printf("\nThe sum of upper triangular matrix is %d",upsum);
}


Enter the number of rows and column: 3 3

Enter the elements of the matrix: 1 2 3 4 5 6 7 8 9

The elements in the matrix are:
1 2 3
4 5 6
7 8 9

The sum of upper triangular matrix is 26