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.print("Enter no of rows or columns : ");
- int s = sc.nextInt();
- int[][] matrix= new int[s][s];
- for(int i = 0; i < s; i++)
- {
- for(int j = 0; j < s; j++)
- {
- System.out.print("Enter value at "+ i + ", " + j +" : ");
- matrix[i][j] = sc.nextInt();
- }
- }
- System.out.println("\n\nThe given matrix : ");
- for(int i = 0; i < s; i++)
- {
- for(int j = 0; j < s; j++)
- {
- System.out.print(matrix[i][j]+" ");
- }
- System.out.println();
- }
- System.out.println("\n\nThe Transpose : ");
- for(int i = 0; i < s; i++)
- {
- for(int j = 0; j < s; j++)
- {
- System.out.print(matrix[j][i]+" ");
- }
- System.out.println();
- }
- int diag1 = 0, diag2 = 0;
- for(int i = 0; i < s; i++)
- {
- diag1 = diag1 + matrix[i][i];
- }
- System.out.println("\n\nThe trace is : " + diag1);
- for(int i = 0, j = s-1; i < s; i++, j--)
- {
- diag2 = diag2 + matrix[i][j];
- }
- System.out.println("\n\nThe sum of other diagonal is : " + diag2);
- System.out.println("\n\nThe sum of 2 diagonals is : " + (diag1 + diag2));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement