 ## Super friendly code & developer communities

A place for coders and developers to share, learn and grow together.

# Java Programming | Creating matrix using 2d arrays

This is a tutorial for creating a matrix using 2d arrays. The program is given below that creates matrix of numbers given by user. The program is extendable. Go enjoy the program. Lets begin…

Program for creating a matrix using 2d arrays.

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

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

//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");
}
}
}
``````

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

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.

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.