Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String...args) {
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter rows/cols : "); //rows and
- columns in matrix must be same.
- int rows = scanner.nextInt();
- int columns=rows;
- int[][] matrix = new int[rows][rows];
- System.out.println("Elements of 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 elements above diagonal.
- int sum=0;
- for (int j = 1; j < columns; j++) {
- for (int i=j-1 ; i>=0 ; i--) {
- sum= sum + matrix[i][j];
- }
- }
- int sumBelow=0;
- for (int i = 1; i < rows; i++) {
- for (int j=i-1 ; j>=0 ; j--) {
- sumBelow= sumBelow+ matrix[i][j];
- }
- }
- System.out.println("Matrix 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 above: "+sum);
- System.out.println("suma below: " + sumBelow );
- int subtract;
- if(sum > sumBelow) {
- razlika = sum - sumBelow;
- System.out.println("Subtracted: " + subtract);
- }else {
- subtract= sumBelow- sum;
- System.out.println("Subtracted: " + subtract);
- }
- }
Add Comment
Please, Sign In to add comment