Java program to print 3×3 matrix

Let’s learn java program to print 3×3 matrix.

Java program to print 3×3 matrix

Here’s 3×3 matrix in java example.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class JavaPrint3x3Matrix
{
   public static void main(String[] args) throws IOException 
   {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      int[][] inputNumber = new int[3][3];
      int a, b;
      String strMatrix;
      System.out.println("Please enter elements to print 3x3 matrix: ");
      for(a = 0; a <= 2; a++)
      {
         for(b = 0; b <= 2; b++)
         {
            strMatrix = br.readLine();
            inputNumber[a][b] = Integer.parseInt(strMatrix);
         }
      }
      System.out.println("Elements in 3x3 matrix are: ");
      System.out.println("");
      for(a = 0; a <= 2; a++)
      {
         for(b = 0; b <= 2; b++)
         {
            System.out.print(inputNumber[a][b] + "\t");
         }
         System.out.println();
      }
   }
}


Output:

Please enter elements to print 3×3 matrix:
2
4
6
8
1
3
5
7
9

Please enter elements to print 3×3 matrix:
2
4
6
8
1
3
5
7
9
Elements in 3×3 matrix are:

2 4 6
8 1 3
5 7 9


Also read – for loop in java