Subtraction of Matrix in Java Program

Simple Subtraction of Matrix in Java Program. Subtraction is the process of subtracting the difference between two numbers. The symbol between the and is called the minus sign, and the symbol between the and is called the minuend. The word ” ” should be rewritten as ” minus “.

public class Main
{
public static void main(String[] args)
{
int rows, cols;
int a[][] = {
{3, 4, 5},
{3, 4, 1},
{1, 2, 3}
};
int b[][] = {
{6, 7, 1},
{2, 3, 1},
{1, 1, 1}
};
rows = a.length;
cols = a[0].length;
int diff[][] = new int[rows][cols];
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
diff[i][j] = a[i][j] - b[i][j];
}
}
System.out.println("Subtraction of two matrices: ");
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
System.out.print(diff[i][j] + " ");
}
System.out.println();
}
}
}

 

Output:

Subtraction of two matrices:
-3 -3 4
1 1 0
0 1 2

What is Rows and Columns in Java Program

Rows are a horizontally aligned set of cells that create consistency. Columns are vertically aligned groups of cells that run from top to bottom.

What is for loop and how to implement in Java Program

A for loop is a repetition control structure that allows you to design a loop that must be repeated a certain number of times quickly. When you know how many times a task will be repeated, a for loop comes in handy.

In Java, the “for” loop is an entry-controlled loop that allows a user to iteratively run a block of statement(s) for a set number of times. The test-condition specified inside the “for” loop determines the number of iterations. The Java “for” loop is one of the most simple Java loops to grasp.

Subtraction of Matrix in Java Program to Matrix Program

“A matrix is a set of numbers organized into a predetermined number of rows and columns.” These are usually real numbers. A two-dimensional array has two dimensions, one for each row and one for each column. An array of arrays can be defined as a two-dimensional array.

The matrices that make up the 2D array are represented as a collection of rows and columns. 2D arrays, on the other hand, are used to build a data structure that resembles that of a relational database.

Compiler: Click Here