Java Programming | Transpose of matrix using 2d arrays

Introduction

This is a tutorial for transpose of matrix using 2d arrays.The program is given below that make transpose of given matrix. The program is extendable. Go enjoy the program. Lets begin……..

Program for transpose of matrix using 2d arrays.

//import Scanner as we require it.
import java.util.Scanner;

// the name of our class its public
public class MatrixTranspose {
	//void main
		public static void main (String[] args)
		{
			//declare int
			int i,j,a[][] = new int[10][10];

			//Declare input as scanner
			Scanner input = new Scanner(System.in);

			//Take input for arrays
			for(i=0;i<3;i++)
			{
				for(j=0;j<3;j++)
				{
					System.out.println("Enter Number :");
					a[i][j] = input.nextInt();
				}
			}

			System.out.println("Matrix :-");
			//print the matrix
			for(i=0;i<3;i++)
			{
				for(j=0;j<3;j++)
					System.out.print(a[i][j]+" ");
				System.out.print("\n");
			}

			System.out.println("Transpose of Matrix :-");
			//print the transpose matrix
			for(j=0;j<3;j++)
			{
				for(i=0;i<3;i++)
					System.out.print(a[i][j]+" ");
				System.out.print("\n");
			}
		}
}

Output

Enter Number :
1
Enter Number :
2
Enter Number :
3
Enter Number :
4
Enter Number :
5
Enter Number :
6
Enter Number :
7
Enter Number :
8
Enter Number :
9
Matrix :-
1 2 3
4 5 6
7 8 9
Transpose of Matrix :-
1 4 7
2 5 8
3 6 9

How does it work

  1. You enter the number.
  2. The number is saved in respective array.
  3. The numbers are taken are printed to form matrix.
  4. Then numbers are printed to form transpose of matrix.

Extending it

The program can be extended by using more numbers and making the matrix more bigger.

Explanation.

  1. Import the Scanner.
  2. Declare the class as public
  3. Add the void main function
  4. Declare input as Scanner.
  5. Add two for loops.
  6. Add system.out.println() function with the message to enter number.
  7. Take the inputs and save it in arrays.
  8. Add two more loop to print the matrix.
  9. Add system.out.print() function to print \n to break line.

Repeat 8 and 9 To print transpose of matrix.

At the end.

You learnt creating the Java program for Creating Transpose matrix using 2d arrays. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s