Associated Course(s):
Geeks Classes in Noida

Given a square matrix** mat[][] **of size** N*N**, print the **sum of upper and lower triangular elements**. Upper Triangle consists of elements on the diagonal and above it. Lower triangle consists of elements on the diagonal and below it.

**Input:**

The first line consists of an integer **T** i.e number of test cases. The first line of each test case consists of an integer **N **denoting the size of the matrix. The next line contains **N*N** spaced separated integers.

**Output:**

For each testcase, in a new line, print the required output.

**Constraints:**

1 <= T <= 100

1 <= N <= 100

1 <= mat[i][j] <= 100

**Example:
Input:**

2

3

6 5 4 1 2 5 7 9 7

2

5 7 8 9

29 32

21 22

**Explanation:
Testcase1: **The given matrix is

6 5 4

1 2 5

7 9 7

The elements of upper triangle are

6 5 4

2 5

7

Sum of these elements is 6+5+4+2+5+7=29

The elements of lower triangle are

6

1 2

7 9 7

Sum of these elements is 6+1+2+7+9+7= 32

Author: saksham0751

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

VISHESHA SADU | 27 |

ghanshyam648 | 25 |

Biswajit3 | 21 |

karthik_kasaragadda | 20 |

tanya_sharma | 18 |

KshatriyaYash | 1066 |

nikhil_sojan | 978 |

lonecoder | 935 |

mazumderrohit8 | 864 |

SumitSingh27 | 832 |

blackshadows | 5327 |

Ibrahim Nash | 5215 |

akhayrutdinov | 5111 |

mb1973 | 4468 |

Quandray | 4444 |

Login to report an issue on this page.