××How to print matrix in java: In this article we are going to see how we can write a program to print 3*3 Matrix in JAVA language and java program to print matrix or java print matrix.
Java Program to Print 3*3 Matrix
How to print 3*3 matrix in java: A 3*3 Matrix program in java is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.
Let’s understand it in more simpler way.
| A00 A01 A02 | Matrix A = | A10 A11 A12 | | A20 A21 A22 | 3*3
Matrix A
represents a 3*3 matrix.- ‘
A
‘ represents the matrix element - ‘
Aij
‘ represents the matrix element at it’s matrix position/index. - ‘
i
‘ represents the row index - ‘
j
‘ represents the column index - Means
A00=Aij
wherei=0
andj=0
,A01=aij
wherei=0
andj=1
and like this. - Here we have started
row
value from 0 andcolumn
value from 0.
Let’s see different ways to print 3*3 matrix.
- By Static Initialization of Array Elements
- By Dynamic Initialization of Array Elements
- By Using toString() method
Method-1: Java Program to Print 3*3 Matrix By Static Initialization of Array Elements
Approach:
- Initialize and declare an array of size 3×3 matrix in java with array elements.
- Use two for loops to iterate the row and column indexes, and output the array elements.
Program:
public class matrix{ public static void main(String args[]) { int arr[][] = {{1,2,3},{4,5,6},{7,8,9}}; int row, col; System.out.print("The 3x3 matrix elements are : "); // Loop to print the elements for(row=0;row<3;row++) { // Used for formatting System.out.print("\n"); for(col=0;col<3;col++) { System.out.print(arr[row][col]+" "); } } } }
Output: The 3x3 matrix elements are : 1 2 3 4 5 6 7 8 9
Method-2: Java Program to Print 3*3 Matrix By Dynamic Initialization of Array Elements
Approach:
- Initialize an array of size 3×3.
- Ask the user for input.
- Use two for loops to iterate the row and column indexes, and input the array elements.
- Similarly use two for loops to output the array elements.
Program:
import java.util.Scanner; public class Matrix { public static void main(String args[]) { //Scanner class to take input Scanner scan = new Scanner(System.in); int arr[][] = new int[3][3]; System.out.print("Enter the 3x3 matrix elements :"); int row, col; // Loop to take user input for(row=0;row<3;row++) for(col=0;col<3;col++) arr[row][col] = scan.nextInt(); System.out.print("The 3x3 matrix elements are : "); // Loop to print the elements for(row=0;row<3;row++) { // Used for formatting System.out.print("\n "); for(col=0;col<3;col++) { System.out.print(arr[row][col]+" "); } } } }
Output: Enter the 3x3 matrix elements : 10 20 30 40 50 60 70 80 90 The 3x3 matrix elements are : 10 20 30 40 50 60 70 80 90
Methoid-3: Java Program to Print 3*3 Matrix By Using toString() method
Approach:
- Initialize an array of size 3×3 or 3×3 array java.
- Ask the user for input.
- Use two for loops to iterate the row and column indexes, and input the array elements.
- Then iterate each row of matrix and print each row of matrix, convert it to String and then print it in separate lines.
Program:
import java.util.*; import java.io.*; public class Matrix { public static void main(String args[]) { //Scanner class to take input Scanner scan = new Scanner(System.in); int arr[][] = new int[3][3]; System.out.print("Enter the 3x3 matrix elements :"); int row, col; // Loop to take user input for(row=0;row<3;row++) for(col=0;col<3;col++) arr[row][col] = scan.nextInt(); System.out.println("The 3x3 matrix elements are : "); //printing the matrix // loop to cover all rows for (int[] r : arr) // first it converts each row as string using toString() method // and then prints that string in a separate line System.out.println(Arrays.toString(r)); } }
Output: Enter the 3x3 matrix elements : 10 20 30 40 50 60 70 80 90 The 3x3 matrix elements are : [10, 20, 30] [40, 50, 60] [70, 80, 90]
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills. This is how java program to print 3×3 Matrix or Print matrix java.
Recommended Reading On: Java Program to Print Matrix in Z form
Solve these:
- Determinant Of A Matrix In Java
- 3*3 Matrix Addition Program In Java
- Java Program To Print A Matrix
- 3 By 3 Matrix
Related Java Programs: