In this section, you will learn how to create a matrix of any order Using Java Programming.
In this section, you will learn how to create a matrix of any order Using Java Programming.A matrix is a rectangular array of numbers.The numbers in the matrix are called its entries or its elements. A matrix with m rows and n columns is called an m-by-n matrix or m × n matrix, while m and n are called its dimensions.Here we have allowed the user to enter the number of rows and columns and the elements they wish to be in the matrix.
2x2 Matrix:
1 2
3 4
3x3 Matrix:
1 2 3
2 3 4
4 5 6
Here is the code:
import java.io.*;
import java.util.*;
public class matrix {
public static void main(String[] args) throws IOException {
int[][] dim = new int[2][2];
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the rows of matrix : ");
dim[0][0] = Integer.parseInt(stdin.readLine());
System.out.println("Enter the columns of matrix : ");
dim[0][1] = Integer.parseInt(stdin.readLine());
int[][] A = new int[dim[0][0]][dim[0][1]];
for (int i = 0; i < A.length; i++)
for (int j = 0; j < A[i].length; j++) {
System.out.println("Enter element ("+(i+1)+","+(j+ 1)1)+"): ");
A[i][j] = Integer.parseInt(stdin.readLine());
}
System.out.println("Matrix ");
for (int i = 0; i < A.length; i++) {
System.out.println();
for (int j = 0; j < A[i].length; j++) {
System.out.print(A[i][j] + " ");
}
}
}
}