Finding the matrix transposition
Here’s an example Java program that allows you to find 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:"); // Read matrix elements from the user for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { matrix[i][j] = scanner.nextInt(); } } int[][] transpose = new int[columns][rows]; // Calculate the transpose of the matrix for (int i = 0; i < columns; i++) { for (int j = 0; j < rows; j++) { transpose[i][j] = matrix[j][i]; } } System.out.println("The transpose of the matrix is:"); // Print the transpose matrix for (int i = 0; i < columns; i++) { for (int j = 0; j < rows; j++) { System.out.print(transpose[i][j] + " "); } System.out.println(); } scanner.close(); } }
This program prompts the user to enter the number of rows and columns of a matrix, followed by the elements of the matrix. It then calculates the transpose of the matrix and displays the result.
No comments yet! You be the first to comment.