Java program to add, subtract and multiply 2 dimensional array

The blog provides the technical solution for w dimensional arrays in java. 2 dimensional arrays are the most important  java array topic in interviews for java developers. Java Array questions like ” how to find multiplication of two matrix in java arrays” , ” java program to add / multiply two array matrix and shows the addition in another dimensional array” , “what could be the result when two array matrix are subtracted” 

Problem Statement: 

How to add , subtract or multiply Two 2-dimensional arrays in java ?

Solution Approach: 

  • Ask user to enter the matrices count ( 2-dimemsional array as Input) 
  • Get the Array elements  (rows and columns) as User Input
  • Initialize the arrays with received count
int n; // array size
//Scanner Object to read user input
Scanner scan = new Scanner(System.in);
n = scan.nextInt();

//create 2 dimensional array (matrix) objects
int[][] TwoDimensionalInputArray_1 = new int[n][n];
int[][] TwoDimensionalInputArray_2 = new int[n][n];
int[][] TwoDimensionalResultArray_3 = new int[n][n];
  • Read the Array elements and set the values in the 2-dimensional array for 1st Array Matrix
for (int incr = 0; incr < n; incr++)
		{
			for (int innerIncr = 0; innerIncr < n; innerIncr++)
			{
				TwoDimensionalInputArray_1[incr][innerIncr] = scan.nextInt();
			}
		}
  • Read the Array elements and set the values in the 2-dimensional array for 1st Array Matrix
for (int incr = 0; incr < n; incr++)
		{
			for (int innerIncr = 0; innerIncr < n; innerIncr++)
			{
				TwoDimensionalInputArray_2[incr][innerIncr] = scan.nextInt();
			}
		}
for (int i = 0; i < n; i++)  // Iterate elements of 1st Input Array
		{
			for (int j = 0; j < n; j++)   // Iterate elements of 2nd Input Array
			{
				for (int k = 0; k < n; k++) // // Iterate elements of Result Array
				{
					TwoDimensionalResultArray_3[i][j] = 
                      TwoDimensionalResultArray_3[i][j] + 
                      TwoDimensionalInputArray_1[i][k] * 
                      TwoDimensionalInputArray_2[k][j];
				}
			}
		}
for (int i = 0; i < n; i++)  // Iterate elements of 1st Input Array
		{
			for (int j = 0; j < n; j++)   // Iterate elements of 2nd Input Array
			{
				for (int k = 0; k < n; k++) // // Iterate elements of Result Array
				{
					TwoDimensionalResultArray_3[i][j] = 
                      TwoDimensionalInputArray_1[i][k] + 
                      TwoDimensionalInputArray_2[k][j];
				}
			}
		}
for (int i = 0; i < n; i++)  // Iterate elements of 1st Input Array
		{
			for (int j = 0; j < n; j++)   // Iterate elements of 2nd Input Array
			{
				for (int k = 0; k < n; k++) // // Iterate elements of Result Array
				{
					TwoDimensionalResultArray_3[i][j] = 
                      TwoDimensionalInputArray_1[i][k] - 
                      TwoDimensionalInputArray_2[k][j];
				}
			}
		}
package com.java.planforexams.array;
import java.util.Scanner;
public class TwoDimensionalArrayMultiply {
	public static void main(String args[])
	{
		int n;
		//Scanner Object to read user input
		Scanner scan = new Scanner(System.in);
		//Get Array Input for Rows and Columns
		System.out.print("Enter  matrix count : ");
		n = scan.nextInt();
		//create 2 dimensional array (matrix) objects
		int[][] TwoDimensionalInputArray_1 = new int[n][n];
		int[][] TwoDimensionalInputArray_2 = new int[n][n];
		int[][] TwoDimensionalResultArray_3 = new int[n][n];
		//input 1st Matrix
		System.out.println("Enter elements of 1st Matrix row and cols \n");
		for (int incr = 0; incr < n; incr++)
		{
			for (int innerIncr = 0; innerIncr < n; innerIncr++)
			{
				TwoDimensionalInputArray_1[incr][innerIncr] = scan.nextInt();
			}
		}
		// input 2nd Matrix 
		System.out.println("Enter  elements of 2nd mrtix row and cols  \n");
		for (int incr = 0; incr < n; incr++)
		{
			for (int innerIncr = 0; innerIncr < n; innerIncr++)
			{
				TwoDimensionalInputArray_2[incr][innerIncr] = scan.nextInt();
			}
		}
	
		// Iterate the Array elements for Multiplying Array 1 * Array 2 
		System.out.println("Multiplying 2 dimensional arrays...");
		for (int i = 0; i < n; i++)  //Iterate elements of 1st Input Array
		{
			for (int j = 0; j < n; j++)  //Iterate elements of 2nd Input Array
			{
				for (int k = 0; k < n; k++) //Iterate elements of Result Array
				{
					TwoDimensionalResultArray_3[i][j] = 
                      TwoDimensionalResultArray_3[i][j] + 
                      TwoDimensionalInputArray_1[i][k] * 
                      TwoDimensionalInputArray_2[k][j];
				}
			}
		}
		// Print the Result of 2 -Dimensional arrays
		System.out.println("Result of multiplying 2 dimensional arrays : ");
		for (int outerIncr = 0; outerIncr < n; outerIncr++)
		{
			for (int innerIncr = 0; innerIncr < n; innerIncr++)
			{
				System.out.println(TwoDimensionalResultArray_3[outerIncr]
                                 [innerIncr] + " ");
			}
		}
		scan.close();
	}
}
Output:

Leave a Reply

Your email address will not be published. Required fields are marked *

*