Geeksforgeeks

Timer is Running

Associated Course(s):
Geeks Classes
Geeks Classes DSA- Tutorials

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.

jainshubham52535 | 529 |

Pathak1899 | 436 |

yashdip123 | 380 |

kirtidee18 | 377 |

jots62 | 351 |

kirtidee18 | 1057 |

ykaushik | 752 |

aggarwalvatsal5 | 741 |

harbilassingh | 576 |

pmr_3689 | 570 |

blackshadows | 5456 |

Ibrahim Nash | 5450 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4827 |

Login to report an issue on this page.