Square Elements of Two Dimensional Array

This is a simple java program for implementing the two dimensional array program and its square. This session provide you the best explanation with mathematical operation.

Square Elements of Two Dimensional Array 

     

This is a simple java program for implementing the two dimensional array program and its square. This session provide you the best explanation with mathematical operation. 

Description of progarm:

We are going to display the square of two matrix. Firstly, we have to define a class "SquareMatrix".  Then we take an integer type array that contains integer type values. After this, we use two 'for' loop that denotes rows and columns of a matrix. After getting both the matrix with us we need to square both matrix. When we go to square this array then we use "square[i][j] =square[i][j] * square[i][j]". So, the output will be displayed on the screen command prompt by using the println() method.

Here is the code of this program

public class SquareMatrix {
  public static void main(String[] args) {
  int square[][]{{1,3,5},{2,4,6}};
  System.out.println("Your Original Matrix: ");
  for(int i = 0; i < 2; i++){
  for(int j = 0; j < 3; j++){
  System.out.print(square[i][j" ");
  }
  System.out.println();
  }
  for(int i = 0; i <= 1; i++) {
  for(int j = 0; j <= 2; j++) {
  square[i][j= square[i][j* square[i][j];
  
  }
  System.out.println("Matrix after changes: ");
  for(int i = 0; i < 2; i++){
  for(int j = 0; j < 3; j++){
  System.out.print(square[i][j" ");
  }
  System.out.println();
  }
  }
}

 Download this Example  

Ads