Write a java program to find the transpose of given matrix using arrays.
Write a java program to find the transpose of given matrix using arrays.
Program:
import java.util.*;
public class matrixtrans
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the rows and columns of the matrix :");
int r=sc.nextInt();
int c=sc.nextInt();
int i, j;
int a[ ][ ]=new int[r][c];
System.out.println("Enter the elements for matrix:");
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Before transose of matrix:");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
System.out.println("After transpose of matrix:");
for(i=0;i<c;i++)
{
for(j=0;j<r;j++){
System.out.print(a[j][i]+ "\t");
}
System.out.println();
}
}
}
Output :
Enter the rows and columns of the matrix :
2
2
Enter the elements for matrix:
1
2
3
4
Before transose of matrix:
1 2
3 4
After transpose of matrix:
1 3
2 4
Comments
Post a Comment