Sum of upper triangle and lower triangle
Here given code implementation process.
// C Program
// Sum of upper triangle and lower triangle
#include <stdio.h>
#define N 4
void triangleSum(int matrix[N][N])
{
int lower = 0;
int upper = 0;
// This is row controlling loop
for (int i = 0; i < N; ++i)
{
// This is column controlling loop
for (int j = 0; j < N; ++j)
{
if(i<=j)
{
// Upper triangle matrix
upper += matrix[i][j];
}
if(j<=i)
{
// Lower triangle matrix
lower += matrix[i][j];
}
}
}
// Display calculated result
printf("\n Upper triangle sum : %d ",upper);
printf("\n Lower triangle sum : %d ",lower);
}
int main(int argc, char const *argv[])
{
int matrix[N][N] =
{
{ 8, 5, 4 , 3 },
{ 1, 1, 5 , 2},
{ 0, -2, 7 , 1 },
{ 3, 2, 4 , 1 }
};
// Test
/*
Lower triangle
--------------
{ 8, 5, 4 , 3 },
{ 1, 5 , 2},
{ 7 , 1 },
{ 1 }
Sum
[
8 + 5 + 4 + 3 + 1 +
5 + 2 + 7 + 1 + 1
]
Output = 37
Upper triangle
--------------
{ 8, },
{ 1, 1, },
{ 0, -2, 7 },
{ 3, 2, 4 , 1 }
Sum
[
8 + 1 + 1 + 0 + (-2) +
7 + 3 + 2 + 4 + 1
]
Output = 25
*/
triangleSum(matrix);
return 0;
}
input
Upper triangle sum : 37
Lower triangle sum : 25
/*
Java Program
Print symmetric double triangle pattern
*/
public class MatrixSum
{
public void triangleSum(int[][] matrix, int row, int col)
{
int lower = 0;
int upper = 0;
// This is row controlling loop
for (int i = 0; i < row; ++i)
{
// This is column controlling loop
for (int j = 0; j < col; ++j)
{
if (i <= j)
{
// Upper triangle matrix
upper += matrix[i][j];
}
if (j <= i)
{
// Lower triangle matrix
lower += matrix[i][j];
}
}
}
// Display calculated result
System.out.print("\n Upper triangle sum : " + upper);
System.out.print("\n Lower triangle sum : " + lower);
}
public static void main(String[] args)
{
MatrixSum task = new MatrixSum();
int[][] matrix = {
{
8 , 5 , 4 , 3
},
{
1 , 1 , 5 , 2
},
{
0 , -2 , 7 , 1
},
{
3 , 2 , 4 , 1
}
};
// Get the size of dimension
int r = matrix.length;
int c = matrix[0].length;
// Test
/*
Lower triangle
--------------
{{ 8, 5, 4 , 3 },
{ 1, 5 , 2},
{ 7 , 1 },
{ 1 }}
Sum
[
8 + 5 + 4 + 3 + 1 +
5 + 2 + 7 + 1 + 1
]
Output = 37
Upper triangle
--------------
{{ 8, },
{ 1, 1, },
{ 0, -2, 7 },
{ 3, 2, 4 , 1 }}
Sum
[
8 + 1 + 1 + 0 + (-2) +
7 + 3 + 2 + 4 + 1
]
Output = 25
*/
task.triangleSum(matrix, r, c);
}
}
input
Upper triangle sum : 37
Lower triangle sum : 25
// Include header file
#include <iostream>
#define N 4
using namespace std;
/*
C++ Program
Print symmetric double triangle pattern
*/
class MatrixSum
{
public: void triangleSum(int matrix[N][N])
{
int lower = 0;
int upper = 0;
// This is row controlling loop
for (int i = 0; i < N; ++i)
{
// This is column controlling loop
for (int j = 0; j < N; ++j)
{
if (i <= j)
{
// Upper triangle matrix
upper += matrix[i][j];
}
if (j <= i)
{
// Lower triangle matrix
lower += matrix[i][j];
}
}
}
// Display calculated result
cout << "\n Upper triangle sum : " << upper;
cout << "\n Lower triangle sum : " << lower;
}
};
int main()
{
MatrixSum *task = new MatrixSum();
int matrix[N][N] = {
{
8 , 5 , 4 , 3
} , {
1 , 1 , 5 , 2
} , {
0 , -2 , 7 , 1
} , {
3 , 2 , 4 , 1
}
};
// Test
/*
Lower triangle
--------------
{{ 8, 5, 4 , 3 },
{ 1, 5 , 2},
{ 7 , 1 },
{ 1 }}
Sum
[
8 + 5 + 4 + 3 + 1 +
5 + 2 + 7 + 1 + 1
]
Output = 37
Upper triangle
--------------
{{ 8, },
{ 1, 1, },
{ 0, -2, 7 },
{ 3, 2, 4 , 1 }}
Sum
[
8 + 1 + 1 + 0 + (-2) +
7 + 3 + 2 + 4 + 1
]
Output = 25
*/
task->triangleSum(matrix);
return 0;
}
input
Upper triangle sum : 37
Lower triangle sum : 25
// Include namespace system
using System;
/*
Csharp Program
Print symmetric double triangle pattern
*/
public class MatrixSum
{
public void triangleSum(int[,] matrix, int row, int col)
{
int lower = 0;
int upper = 0;
// This is row controlling loop
for (int i = 0; i < row; ++i)
{
// This is column controlling loop
for (int j = 0; j < col; ++j)
{
if (i <= j)
{
// Upper triangle matrix
upper += matrix[i,j];
}
if (j <= i)
{
// Lower triangle matrix
lower += matrix[i,j];
}
}
}
// Display calculated result
Console.Write("\n Upper triangle sum : " + upper);
Console.Write("\n Lower triangle sum : " + lower);
}
public static void Main(String[] args)
{
MatrixSum task = new MatrixSum();
int[,] matrix = {
{
8 , 5 , 4 , 3
},
{
1 , 1 , 5 , 2
},
{
0 , -2 , 7 , 1
},
{
3 , 2 , 4 , 1
}
};
// Get the size of dimension
int r = matrix.GetLength(0);
int c = matrix.GetLength(1);
// Test
/*
Lower triangle
--------------
{{ 8, 5, 4 , 3 },
{ 1, 5 , 2},
{ 7 , 1 },
{ 1 }}
Sum
[
8 + 5 + 4 + 3 + 1 +
5 + 2 + 7 + 1 + 1
]
Output = 37
Upper triangle
--------------
{{ 8, },
{ 1, 1, },
{ 0, -2, 7 },
{ 3, 2, 4 , 1 }}
Sum
[
8 + 1 + 1 + 0 + (-2) +
7 + 3 + 2 + 4 + 1
]
Output = 25
*/
task.triangleSum(matrix, r, c);
}
}
input
Upper triangle sum : 37
Lower triangle sum : 25
<?php
/*
Php Program
Print symmetric double triangle pattern
*/
class MatrixSum
{
public function triangleSum($matrix, $row, $col)
{
$lower = 0;
$upper = 0;
// This is row controlling loop
for ($i = 0; $i < $row; ++$i)
{
// This is column controlling loop
for ($j = 0; $j < $col; ++$j)
{
if ($i <= $j)
{
// Upper triangle matrix
$upper += $matrix[$i][$j];
}
if ($j <= $i)
{
// Lower triangle matrix
$lower += $matrix[$i][$j];
}
}
}
// Display calculated result
echo("\n Upper triangle sum : ".$upper);
echo("\n Lower triangle sum : ".$lower);
}
}
function main()
{
$task = new MatrixSum();
$matrix = array(
array(8, 5, 4, 3),
array(1, 1, 5, 2),
array(0, -2, 7, 1),
array(3, 2, 4, 1)
);
// Get the size of dimension
$r = count($matrix);
$c = count($matrix[0]);
// Test
/*
Lower triangle
--------------
{{ 8, 5, 4 , 3 },
{ 1, 5 , 2},
{ 7 , 1 },
{ 1 }}
Sum
[
8 + 5 + 4 + 3 + 1 +
5 + 2 + 7 + 1 + 1
]
Output = 37
Upper triangle
--------------
{{ 8, },
{ 1, 1, },
{ 0, -2, 7 },
{ 3, 2, 4 , 1 }}
Sum
[
8 + 1 + 1 + 0 + (-2) +
7 + 3 + 2 + 4 + 1
]
Output = 25
*/
$task->triangleSum($matrix, $r, $c);
}
main();
input
Upper triangle sum : 37
Lower triangle sum : 25
/*
Node JS Program
Print symmetric double triangle pattern
*/
class MatrixSum
{
triangleSum(matrix, row, col)
{
var lower = 0;
var upper = 0;
// This is row controlling loop
for (var i = 0; i < row; ++i)
{
// This is column controlling loop
for (var j = 0; j < col; ++j)
{
if (i <= j)
{
// Upper triangle matrix
upper += matrix[i][j];
}
if (j <= i)
{
// Lower triangle matrix
lower += matrix[i][j];
}
}
}
// Display calculated result
process.stdout.write("\n Upper triangle sum : " + upper);
process.stdout.write("\n Lower triangle sum : " + lower);
}
}
function main()
{
var task = new MatrixSum();
var matrix = [
[8, 5, 4, 3],
[1, 1, 5, 2],
[0, -2, 7, 1],
[3, 2, 4, 1]
];
// Get the size of dimension
var r = matrix.length;
var c = matrix[0].length;
// Test
/*
Lower triangle
--------------
{{ 8, 5, 4 , 3 },
{ 1, 5 , 2},
{ 7 , 1 },
{ 1 }}
Sum
[
8 + 5 + 4 + 3 + 1 +
5 + 2 + 7 + 1 + 1
]
Output = 37
Upper triangle
--------------
{{ 8, },
{ 1, 1, },
{ 0, -2, 7 },
{ 3, 2, 4 , 1 }}
Sum
[
8 + 1 + 1 + 0 + (-2) +
7 + 3 + 2 + 4 + 1
]
Output = 25
*/
task.triangleSum(matrix, r, c);
}
main();
input
Upper triangle sum : 37
Lower triangle sum : 25
# Python 3 Program
# Print symmetric double triangle pattern
class MatrixSum :
def triangleSum(self, matrix, row, col) :
lower = 0
upper = 0
i = 0
# This is row controlling loop
while (i < row) :
j = 0
# This is column controlling loop
while (j < col) :
if (i <= j) :
# Upper triangle matrix
upper += matrix[i][j]
if (j <= i) :
# Lower triangle matrix
lower += matrix[i][j]
j += 1
i += 1
# Display calculated result
print("\n Upper triangle sum : ", upper, end = "")
print("\n Lower triangle sum : ", lower, end = "")
def main() :
task = MatrixSum()
matrix = [
[8, 5, 4, 3],
[1, 1, 5, 2],
[0, -2, 7, 1],
[3, 2, 4, 1]
]
# Get the size of dimension
r = len(matrix)
c = len(matrix[0])
# Test
# Lower triangle
# --------------
# [
# [ 8, 5, 4 , 3]
# [ 1, 5 , 2 ]
# [ 7 , 1 ]
# [ 1 ] ]
# Sum
# [
# 8 + 5 + 4 + 3 + 1 +
# 5 + 2 + 7 + 1 + 1
# ]
# Output = 37
# Upper triangle
# --------------
# [[ 8, ]
# [ 1, 1, ]
# [ 0, -2, 7 ]
# [ 3, 2, 4 , 1 ]]
# Sum
# [
# 8 + 1 + 1 + 0 + (-2) +
# 7 + 3 + 2 + 4 + 1
# ]
# Output = 25
task.triangleSum(matrix, r, c)
if __name__ == "__main__": main()
input
Upper triangle sum : 37
Lower triangle sum : 25
# Ruby Program
# Print symmetric double triangle pattern
class MatrixSum
def triangleSum(matrix, row, col)
lower = 0
upper = 0
i = 0
# This is row controlling loop
while (i < row)
j = 0
# This is column controlling loop
while (j < col)
if (i <= j)
# Upper triangle matrix
upper += matrix[i][j]
end
if (j <= i)
# Lower triangle matrix
lower += matrix[i][j]
end
j += 1
end
i += 1
end
# Display calculated result
print("\n Upper triangle sum : ", upper)
print("\n Lower triangle sum : ", lower)
end
end
def main()
task = MatrixSum.new()
matrix = [
[8, 5, 4, 3],
[1, 1, 5, 2],
[0, -2, 7, 1],
[3, 2, 4, 1]
]
r = matrix.length
c = matrix[0].length
# Test
# Lower triangle
# --------------
# [[ 8, 5, 4 , 3 ],
# [ 1, 5 , 2],
# [ 7 , 1 ],
# [ 1 ]]
# Sum
# [
# 8 + 5 + 4 + 3 + 1 +
# 5 + 2 + 7 + 1 + 1
# ]
# Output = 37
# Upper triangle
# --------------
# [[ 8, ],
# [ 1, 1, ],
# [ 0, -2, 7 ],
# [ 3, 2, 4 , 1 ]]
# Sum
# [
# 8 + 1 + 1 + 0 + (-2) +
# 7 + 3 + 2 + 4 + 1
# ]
# Output = 25
task.triangleSum(matrix, r, c)
end
main()
input
Upper triangle sum : 37
Lower triangle sum : 25
/*
Scala Program
Print symmetric double triangle pattern
*/
class MatrixSum()
{
def triangleSum(matrix: Array[Array[Int]], row: Int, col: Int): Unit = {
var lower: Int = 0;
var upper: Int = 0;
var i: Int = 0;
// This is row controlling loop
while (i < row)
{
var j: Int = 0;
// This is column controlling loop
while (j < col)
{
if (i <= j)
{
// Upper triangle matrix
upper += matrix(i)(j);
}
if (j <= i)
{
// Lower triangle matrix
lower += matrix(i)(j);
}
j += 1;
}
i += 1;
}
// Display calculated result
print("\n Upper triangle sum : " + upper);
print("\n Lower triangle sum : " + lower);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: MatrixSum = new MatrixSum();
var matrix: Array[Array[Int]] = Array(
Array(8, 5, 4, 3),
Array(1, 1, 5, 2),
Array(0, -2, 7, 1),
Array(3, 2, 4, 1)
);
var r: Int = matrix.length;
var c: Int = matrix(0).length;
// Test
/*
Lower triangle
--------------
[[ 8, 5, 4 , 3 ],
[ 1, 5 , 2],
[ 7 , 1 ],
[ 1 ]]
Sum
[
8 + 5 + 4 + 3 + 1 +
5 + 2 + 7 + 1 + 1
]
Output = 37
Upper triangle
--------------
[[ 8, ],
[ 1, 1, ],
[ 0, -2, 7 ],
[ 3, 2, 4 , 1 ]]
Sum
[
8 + 1 + 1 + 0 + (-2) +
7 + 3 + 2 + 4 + 1
]
Output = 25
*/
task.triangleSum(matrix, r, c);
}
}
input
Upper triangle sum : 37
Lower triangle sum : 25
import Foundation;
/*
Swift 4 Program
Print symmetric double triangle pattern
*/
class MatrixSum
{
func triangleSum(_ matrix: [
[Int]
], _ row: Int, _ col: Int)
{
var lower = 0;
var upper = 0;
var i = 0;
// This is row controlling loop
while (i < row)
{
var j = 0;
// This is column controlling loop
while (j < col)
{
if (i <= j)
{
// Upper triangle matrix
upper += matrix[i][j];
}
if (j <= i)
{
// Lower triangle matrix
lower += matrix[i][j];
}
j += 1;
}
i += 1;
}
// Display calculated result
print("\n Upper triangle sum : ", upper, terminator: "");
print("\n Lower triangle sum : ", lower, terminator: "");
}
}
func main()
{
let task = MatrixSum();
let matrix = [
[8, 5, 4, 3],
[1, 1, 5, 2],
[0, -2, 7, 1],
[3, 2, 4, 1]
];
let r = matrix.count;
let c = matrix[0].count;
// Test
/*
Lower triangle
--------------
[[ 8, 5, 4 , 3 ],
[ 1, 5 , 2],
[ 7 , 1 ],
[ 1 ]]
Sum
[
8 + 5 + 4 + 3 + 1 +
5 + 2 + 7 + 1 + 1
]
Output = 37
Upper triangle
--------------
[[ 8, ],
[ 1, 1, ],
[ 0, -2, 7 ],
[ 3, 2, 4 , 1 ]]
Sum
[
8 + 1 + 1 + 0 + (-2) +
7 + 3 + 2 + 4 + 1
]
Output = 25
*/
task.triangleSum(matrix, r, c);
}
main();
input
Upper triangle sum : 37
Lower triangle sum : 25
/*
Kotlin Program
Print symmetric double triangle pattern
*/
class MatrixSum
{
fun triangleSum(matrix: Array < Array < Int >> , row: Int, col: Int): Unit
{
var lower: Int = 0;
var upper: Int = 0;
var i: Int = 0;
// This is row controlling loop
while (i < row)
{
var j: Int = 0;
// This is column controlling loop
while (j < col)
{
if (i <= j)
{
// Upper triangle matrix
upper += matrix[i][j];
}
if (j <= i)
{
// Lower triangle matrix
lower += matrix[i][j];
}
j += 1;
}
i += 1;
}
// Display calculated result
print("\n Upper triangle sum : " + upper);
print("\n Lower triangle sum : " + lower);
}
}
fun main(args: Array < String > ): Unit
{
val task: MatrixSum = MatrixSum();
val matrix: Array < Array < Int >> = arrayOf(
arrayOf(8, 5, 4, 3),
arrayOf(1, 1, 5, 2),
arrayOf(0, -2, 7, 1),
arrayOf(3, 2, 4, 1)
);
val r: Int = matrix.count();
val c: Int = matrix[0].count();
// Test
/*
Lower triangle
--------------
[[ 8, 5, 4 , 3 ],
[ 1, 5 , 2],
[ 7 , 1 ],
[ 1 ]]
Sum
[
8 + 5 + 4 + 3 + 1 +
5 + 2 + 7 + 1 + 1
]
Output = 37
Upper triangle
--------------
[[ 8, ],
[ 1, 1, ],
[ 0, -2, 7 ],
[ 3, 2, 4 , 1 ]]
Sum
[
8 + 1 + 1 + 0 + (-2) +
7 + 3 + 2 + 4 + 1
]
Output = 25
*/
task.triangleSum(matrix, r, c);
}
input
Upper triangle sum : 37
Lower triangle sum : 25
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment