Find sum of lower triangle in matrix - program in java

Write a program to Find sum of lower triangle in matrix.

Sum of lower triangle = 1+5+6+8+7+6+4+3+2+1= 43


Logic behind finding sum of lower triangle in matrix is:
//Logic to calculate sum of lower triangle.
          int sum=0;
          for (int i = 0; i < rows; i++) {
                 for (int j=i ; j>=0 ; j--) {
                       sum= sum + matrix[i][j];
                 }
                
          }



Full Program/SourceCode >
package matrix;
import java.util.Scanner;
/** Copyright (c), AnkitMittal  JavaMadeSoEasy.com */
public class SumOfLowerTriangle {
   public static void main(String...args) {
         
          Scanner scanner = new Scanner(System.in);
          System.out.print("Enter number of rows/columns in matrix : ");    //rows and columns in matrix must be same.
          int rows = scanner.nextInt();
          int columns=rows;
          int[][] matrix = new int[rows][rows];
         
          System.out.println("Enter the elements in matrix :");
          for (int i = 0; i < rows; i++) {
                 for (int j = 0; j < columns; j++) {
                       matrix[i][j] = scanner.nextInt();
                 }
          }
         
          //Logic to calculate sum of lower triangle.
          int sum=0;
          for (int i = 0; i < rows; i++) {
                 for (int j=i ; j>=0 ; j--) {
                       sum= sum + matrix[i][j];
                 }
                
          }
          System.out.println("\nMatrix is : ");
          for (int i = 0; i < rows; i++) {
                 for (int j = 0; j < columns; j++) {
                       System.out.print(matrix[i][j] + " ");
                 }
                 System.out.println();
          }
          System.out.println("sum of lower triangle is: "+sum);
   }
}
/*OUTPUT
Enter number of rows/columns in matrix : 4
Enter the elements in matrix :
1
2
3
4
5
6
7
8
8
7
6
5
4
3
2
1
Matrix is :
1 2 3 4
5 6 7 8
8 7 6 5
4 3 2 1
sum of lower triangle is: 43
*/


Previous program                                                                  Next program


RELATED LINKS>

>Pattern/Pyramid generating programs



eEdit
Must read for you :