Advertisement
KahnClifford

MatrixDiagonalSum

Dec 24th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class matrix {
  3. public static void main(String[] args){
  4.  
  5. Scanner sc = new Scanner(System.in);
  6. System.out.println("Enter a no.: ");
  7. int input = sc.nextInt();
  8. int[][] matrix = new int[input][input];
  9. int sumfd =0;
  10. int sumsd =0;
  11.  
  12. for(int row =0;row<matrix[0].length;row++){
  13.  
  14. for(int col=0;col<matrix[row].length;col++){
  15. matrix[row][col] = sc.nextInt();
  16. }
  17. }
  18.  
  19. for(int roof=0;roof<matrix[0].length+8;roof++){ System.out.print("_"); }
  20. for(int row=0;row<matrix[0].length;row++){
  21. System.out.println();
  22.  
  23. for(int col=0;col<matrix[row].length;col++){
  24. System.out.print(matrix[row][col]);
  25.  
  26. if(row==col){ sumfd += matrix[row][row];}
  27. if(row+col==matrix.length-1) { sumsd += matrix[row][matrix.length-row-1]; }
  28.  
  29. if(matrix[row][col] > 9){
  30. System.out.print("|");
  31. }else{
  32. System.out.print(" |");
  33. }
  34. }
  35.  
  36. }
  37. System.out.println();
  38. for(int floor=0;floor<matrix[0].length+8;floor++){ System.out.print("¯"); }
  39. System.out.println();
  40.  
  41. System.out.println("Sum of first diagonal: "+sumfd);
  42. System.out.println("Sum of second diagonal: "+sumsd);
  43.  
  44. }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement