
write a program in java to declare a square matrices 'A' or order n which is less than 20.allow in user to input only positive integers into the matrix and print the transpose of it.

Java Find Transpose of Matrix
import java.util.*;
public class Transpose {
public static void main(String[] args) throws Exception {
int rows, cols;
int[][] matrix, tmatrix;
Scanner input = new Scanner(System.in);
System.out.print("Enter number of rows: ");
rows = input.nextInt();
System.out.print("Enter number of columns: ");
cols = input.nextInt();
matrix = new int[rows][cols];
tmatrix = new int[cols][rows];
System.out.println("Enter elements for Matrix");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = input.nextInt();
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
tmatrix[i][j] = matrix[j][i];
}
}
System.out.println("Matrix is:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
System.out.println("Its transpose is: ");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
System.out.print(tmatrix[i][j] + " ");
}
System.out.println();
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.