In this BlueJ Program, values are stored in an 2-d array and transpose of the matrix is displayed. The transpose of a matrix is simply a flipped version of the original matrix. We can transpose a matrix by switching its rows with its columns.
Take
values in a n X m matrix and display the
transpose of the matrix
import
java.util.Scanner;
class
Star
{
int
n,m,arr[][],i,j;
Scanner
sc=new Scanner(System.in);
public
void read()
{
System.out.print("\nEnter
value of row: ");
n=sc.nextInt();
System.out.print("\nEnter
value of column: ");
m=sc.nextInt();
arr=new
int [n][m];
for(i=0;i<
n;i++)
{
for(j=0;j< m;j++)
{
System.out.print("\nEnter value:
");
arr[i][j]=sc.nextInt();
}
}
System.out.println("\nOriginal
Matrix ");
for(i=0;i<
n;i++)
{
for(j=0;j< m;j++)
{
System.out.print(" "+arr[i][j]);
}
System.out.println();
}
System.out.println("\nTranspose
Matrix ");
for(i=0;i<
m;i++)
{
for(j=0;j< n;j++)
{
System.out.print(" "+arr[j][i]);
}
System.out.println();
}
}
public
static void main(String args[])
{
Star ob=new Star();
ob.read();
}
}
No comments:
Post a Comment