Sum of upper triangle and lower triangle

Given a matrix print the sum of upper and lower triangular elements (i.e elements on 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

The solution is quite simple, we just need to traverse the matrix and calculate the sum for upper and lower triangles accordingly.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 }};
                       
    /*calling the function*/
    sum(mat, r, c);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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}};
                          
        /*calling the function*/
        sum(mat, r, c);
    }
}
  
// This code is contributed by nitin mittal.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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));
                      
    /*calling the function*/
    sum($mat, $r, $c);
  
// This code is contributed by Sam007
?>

chevron_right



Output :

Upper sum is 29
Lower sum is 32

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

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : nitin mittal, Sam007



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.