The Wayback Machine - https://web.archive.org/web/20221022115615/https://www.geeksforgeeks.org/sum-upper-triangle-lower-triangle/
Skip to content
Related Articles

Related Articles

Sum of upper triangle and lower triangle

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 15 Sep, 2022
View Discussion
Improve Article
Save Article

Given a N x M matrix. The task is to print the sum of upper and lower triangular elements (i.e elements on the diagonal and the upper and lower elements).

Examples : 

Input: {{6, 5, 4}, {1, 2, 5}, {7, 9, 7}}
Output:
Upper sum is 29
Lower sum is 32

Input: {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}
Output:
Upper sum is 10
Lower sum is 14

Approach: To solve the problem follow the below approach

Traverse the matrix and calculate the sum for upper and lower triangles accordingly. 

Below is the implementation of the above approach:

C++




// C++ program to calculate the sum
// of upper and lower triangle
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate sum
void sum(int mat[3][3], int r, int c)
{
    int i, j;
    int upper_sum = 0;
    int lower_sum = 0;
 
    // To calculate sum of upper triangle
    for (i = 0; i < r; i++)
        for (j = 0; j < c; j++) {
            if (i <= j) {
                upper_sum += mat[i][j];
            }
        }
 
    printf("Upper sum is %d\n", upper_sum);
 
    // To calculate sum of lower
    for (i = 0; i < r; i++)
        for (j = 0; j < c; j++) {
            if (j <= i) {
                lower_sum += mat[i][j];
            }
        }
 
    printf("Lower sum is %d", lower_sum);
}
 
// Driver function
int main()
{
    int r = 3;
    int c = 3;
 
    // Giving the matrix
    int mat[3][3]
        = { { 6, 5, 4 }, { 1, 2, 5 }, { 7, 9, 7 } };
 
    // Function Call
    sum(mat, r, c);
    return 0;
}

Java




// Java program to calculate the sum
// of upper and lower triangle
 
class GFG {
    /*Function to calculate sum*/
    static void sum(int mat[][], int r, int c)
    {
        int i, j;
        int upper_sum = 0;
        int lower_sum = 0;
 
        /*Calculate sum of upper triangle*/
        for (i = 0; i < r; i++)
            for (j = 0; j < c; j++) {
                if (i <= j) {
                    upper_sum += mat[i][j];
                }
            }
 
        System.out.println("Upper sum is " + upper_sum);
 
        /*Calculate sum of lower*/
        for (i = 0; i < r; i++)
            for (j = 0; j < c; j++) {
                if (j <= i) {
                    lower_sum += mat[i][j];
                }
            }
 
        System.out.print("Lower sum is " + lower_sum);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int r = 3;
        int c = 3;
 
        /*Giving the matrix*/
        int mat[][]
            = { { 6, 5, 4 }, { 1, 2, 5 }, { 7, 9, 7 } };
 
        /*Calling the function*/
        sum(mat, r, c);
    }
}
 
// This code is contributed by Anant Agarwal.

Python3




# Python3 program to calculate the sum
# of upper and lower triangle
 
# Function to calculate sum
 
 
def Sum(mat, r, c):
 
    i, j = 0, 0
    upper_sum = 0
    lower_sum = 0
 
    # To calculate sum of upper triangle
    for i in range(r):
        for j in range(c):
            if (i <= j):
                upper_sum += mat[i][j]
 
    print("Upper sum is ", upper_sum)
 
    # To calculate sum of lower
    for i in range(r):
        for j in range(c):
            if (j <= i):
                lower_sum += mat[i][j]
 
    print("Lower sum is ", lower_sum)
 
 
# Driver Code
r = 3
c = 3
 
# Giving the matrix
mat = [[6, 5, 4],
       [1, 2, 5],
       [7, 9, 7]]
 
# Function Call
Sum(mat, r, c)
 
# This code is contributed by 29AjayKumar

C#




// C# program to calculate the sum
// of upper and lower triangle
using System;
 
class GFG {
    /*Function to calculate sum*/
    static void sum(int[, ] mat, int r, int c)
    {
        int i, j;
        int upper_sum = 0;
        int lower_sum = 0;
 
        /*Calculate sum of upper triangle*/
        for (i = 0; i < r; i++)
            for (j = 0; j < c; j++) {
                if (i <= j) {
                    upper_sum += mat[i, j];
                }
            }
 
        Console.WriteLine("Upper sum is " + upper_sum);
 
        /*Calculate sum of lower*/
        for (i = 0; i < r; i++)
            for (j = 0; j < c; j++) {
                if (j <= i) {
                    lower_sum += mat[i, j];
                }
            }
 
        Console.Write("Lower sum is " + lower_sum);
    }
 
    // Driver code
    public static void Main()
    {
        int r = 3;
        int c = 3;
 
        /*giving the matrix*/
        int[, ] mat
            = { { 6, 5, 4 }, { 1, 2, 5 }, { 7, 9, 7 } };
 
        // Function Call
        sum(mat, r, c);
    }
}
 
// This code is contributed by nitin mittal.

PHP




<?php
// PHP program to calculate the sum
// of upper and lower triangle
 
// Function to calculate sum
function sum($mat, $r, $c)
{
     
    $upper_sum = 0;
    $lower_sum = 0;
     
    /* To calculate sum of
       upper triangle */
    for ($i = 0; $i < $r; $i++)
        for ($j = 0; $j < $c; $j++)
        {
            if ($i <= $j)
            {
                $upper_sum += $mat[$i][$j];
            }
        }
 
    echo "Upper sum is ". $upper_sum."\n";
     
    /* To calculate sum of lower */
    for ($i = 0; $i < $r; $i++)
        for ($j = 0; $j < $c; $j++)
        {
            if ($j <= $i)
            {
                $lower_sum += $mat[$i][$j];
            }
        }
 
    echo "Lower sum is ". $lower_sum;
}
 
    // Driver Code
    $r = 3;
    $c = 3;
 
    /*Giving the matrix*/
    $mat = array(array(6, 5, 4),
                 array(1, 2, 5),
                 array(7, 9, 7));
                     
    //Function Call
    sum($mat, $r, $c);
 
// This code is contributed by Sam007
?>

Javascript




<script>
 
// Javascript program to calculate the sum
// of upper and lower triangle
 
/*function to calculate sum*/
function sum(mat,r,c)
{
    let i, j;
    let upper_sum = 0;
    let lower_sum = 0;
     
    /*to calculate sum of upper triangle*/
    for (i = 0; i < r; i++)
        for (j = 0; j < c; j++) {
            if (i <= j) {
                upper_sum += mat[i][j];
            }
        }
 
    document.write("Upper sum is "+ upper_sum+"<br/>");
     
    /*to calculate sum of lower*/
    for (i = 0; i < r; i++)
        for (j = 0; j < c; j++) {
            if (j <= i) {
                lower_sum += mat[i][j];
            }
        }
 
    document.write("Lower sum is "+lower_sum);
}
 
/*driver function*/
 
    let r = 3;
    let c = 3;
 
    /*giving the matrix*/
    let mat = [[ 6, 5, 4 ],
                     [ 1, 2, 5 ],
                     [ 7, 9, 7 ]];
                      
    /*calling the function*/
    sum(mat, r, c);
    
// This code contributed by Rajput-Ji
 
</script>

Output

Upper sum is 29
Lower sum is 32

Time Complexity: O(r * c), Where r and c represent the number of rows and columns of the given matrix.
Auxiliary Space: O(1), No extra space is required, so it is a constant.

This article is contributed by Pranav. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!