Finding the transpose of a matrix.
Here’s an example of a Java program that finds the transpose of a matrix:
import java.util.Scanner; public class MatrixTranspose { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the number of rows in the matrix: "); int rows = scanner.nextInt(); System.out.print("Enter the number of columns in the matrix: "); int columns = scanner.nextInt(); int[][] matrix = new int[rows][columns]; System.out.println("Enter the elements of the matrix:"); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { matrix[i][j] = scanner.nextInt(); } } int[][] transposeMatrix = new int[columns][rows]; for (int i = 0; i < columns; i++) { for (int j = 0; j < rows; j++) { transposeMatrix[i][j] = matrix[j][i]; } } System.out.println("The transpose of the matrix is:"); for (int i = 0; i < columns; i++) { for (int j = 0; j < rows; j++) { System.out.print(transposeMatrix[i][j] + " "); } System.out.println(); } scanner.close(); } }
In this program, the user is prompted to enter the number of rows and columns in the matrix. Then, the user can input the elements of the matrix. The program then calculates the transpose of the matrix by swapping the rows and columns. Finally, it prints the transpose matrix to the console.
Please note that this program assumes that the user will input integer values for the matrix elements.
No comments yet! You be the first to comment.