Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class matrix {
- public static void main(String[] args){
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter a no.: ");
- int input = sc.nextInt();
- int[][] matrix = new int[input][input];
- int sumfd =0;
- int sumsd =0;
- for(int row =0;row<matrix[0].length;row++){
- for(int col=0;col<matrix[row].length;col++){
- matrix[row][col] = sc.nextInt();
- }
- }
- for(int roof=0;roof<matrix[0].length+8;roof++){ System.out.print("_"); }
- for(int row=0;row<matrix[0].length;row++){
- System.out.println();
- for(int col=0;col<matrix[row].length;col++){
- System.out.print(matrix[row][col]);
- if(row==col){ sumfd += matrix[row][row];}
- if(row+col==matrix.length-1) { sumsd += matrix[row][matrix.length-row-1]; }
- if(matrix[row][col] > 9){
- System.out.print("|");
- }else{
- System.out.print(" |");
- }
- }
- }
- System.out.println();
- for(int floor=0;floor<matrix[0].length+8;floor++){ System.out.print("¯"); }
- System.out.println();
- System.out.println("Sum of first diagonal: "+sumfd);
- System.out.println("Sum of second diagonal: "+sumsd);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement