Note: The transpose of a matrix is a new matrix whose rows are the columns of the original. (This makes the columns of the new matrix the rows of the original).
public class TransposeOfMatrix {
public static void main(String args[]) {
int a[][] = new int[3][3];
int transpose[][] = new int[3][3];
Scanner sc=new Scanner(System.in);
System.out.println("Enter Matrix ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Value of First Matrix is ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
transpose[i][j]=a[j][i];
}
}
System.out.println("Transpose of given Matrix is ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
System.out.print(transpose[i][j]+" ");
}
System.out.println();
}
}
}
Output:
Enter Matrix
1
2
3
1
2
3
1
2
3
Value of First Matrix is
1 2 3
1 2 3
1 2 3
Transpose of given Matrix is
1 1 1
2 2 2
3 3 3
public class TransposeOfMatrix {
public static void main(String args[]) {
int a[][] = new int[3][3];
int transpose[][] = new int[3][3];
Scanner sc=new Scanner(System.in);
System.out.println("Enter Matrix ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Value of First Matrix is ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
transpose[i][j]=a[j][i];
}
}
System.out.println("Transpose of given Matrix is ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
System.out.print(transpose[i][j]+" ");
}
System.out.println();
}
}
}
Output:
Enter Matrix
1
2
3
1
2
3
1
2
3
Value of First Matrix is
1 2 3
1 2 3
1 2 3
Transpose of given Matrix is
1 1 1
2 2 2
3 3 3
|
No comments:
Post a Comment