Advertisement
Vasilena

ProgrammingExercize150221.2

Feb 15th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Programming150221 {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.  
  7.         int i,j,row,col,sum =0;
  8.         System.out.println("Enter the number of rows:");
  9.         row = sc.nextInt();
  10.         System.out.println("Enter the number of columns:");
  11.         col = sc.nextInt();
  12.  
  13.         int[][] mat = new int[row][col];
  14.  
  15.         System.out.println("Enter the elements of the matrix") ;
  16.         for(i=0;i<row;i++) {
  17.             for(j=0;j<col;j++) {
  18.                 mat[i][j] = sc.nextInt();
  19.             }
  20.         }
  21.  
  22.         System.out.println("The elements of the matrix") ;
  23.         for(i=0;i<row;i++) {
  24.             for(j=0;j<col;j++)
  25.             {
  26.                 System.out.print(mat[i][j]+"\t");
  27.             }
  28.             System.out.println("");
  29.         }
  30.  
  31.         for(i=0;i<row;i++) {
  32.             for(j=0;j<col;j++) {
  33.                 if(i==j) {
  34.                     sum = sum + mat[i][j];
  35.                 }
  36.             }
  37.         }
  38.  
  39.         System.out.printf("The SUM of DIAGONAL elements of the matrix = "+sum) ;
  40.  
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement