Sum of Non Lower Triangle Elements:
Sum of Non Lower Triangle Elements:
Program:
import java.util.Scanner;
public class NonLowerTriangleElementsSum {
public static void main(String[] args) {
int r, c;
Scanner s = new Scanner(System.in);
r = s.nextInt();
c = s.nextInt();
int[][] matrix = new int[r][c];
for(int i = 0; i<r; i++) {
for(int j = 0; j<c;j++) {
matrix[i][j] = s.nextInt();
}
}
int sum = 0;
for(int i = 0; i<r; i++) {
for(int j = 0;j<c; j++) {
if(i<j) {
System.out.print(matrix[i][j]);
System.out.print(" ");
sum+=matrix[i][j];
}
}
}
System.out.println("\nThe sum of Non Lower triangular elements of a matrix is: " + sum);
}
}
Output:
3 3
1 2 3 4 5 6 7 8 9
2 3 6
The sum of Non Lower triangular elements of a matrix is: 11
Happy Coding💻:-)
Comments
Post a Comment