Subtraction of two matrix in java

Posted By: Java Examples - 7:26 PM

Share

& Comment

we are going to calculate the subtraction of two matrix and containing its rows and columns. See below for better understanding to this: 
In this program we are going to calculate the  subtraction  of two matrix. To make this program, we need to declare two dimensional array of type integer. Now we need to make a matrix out of it. To make the matrix we will use the for loop.
By making use of the for loop the rows and column will get divide. This process will be performed again for creating the second matrix. After getting both the matrix with us, we need to  subtraction  both the matrix. The both matrix will be subtracted by using the for loop with C[r][c]= A[r][c]-B[r][c]. The output will be displayed by using the print() method.


Java version of the  subtraction of two matrix  problem  is given below: 
/**
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package matrix;

/**
 *
 * @author ACHCHUTHAN
 */
public class SubtractionMatrix {

    int A[][];
    int B[][];
    int C[][];
    int row;
    int coloum;

    public SubtractionMatrix(int n, int m) {
        this.row = n;
        this.coloum = m;
        A = new int[n][m];
        B = new int[n][m];
        C = new int[n][m];
        //assign random value for A,B
        for (int r = 0; r < n; r++) {
            for (int c = 0; c < m; c++) {
                A[r][c] = (int) Math.round(Math.random() * 98 + 10);
                B[r][c] = (int) Math.round(Math.random() * 12);
            }
        }
    }

    public void Subtraction() {
        for (int r = 0; r < row; r++) {
            for (int c = 0; c < coloum; c++) {
                C[r][c] = A[r][c] - B[r][c];
            }
        }
    }

    public void print() {
        for (int r = 0; r < row; r++) {
            for (int c = 0; c < coloum; c++) {
                System.out.print(C[r][c] + "\t");
            }
            System.out.println();
        }
    }

    public static void main(String args[]) {
        System.out.println("Subtraction of A,B :");
        SubtractionMatrix S = new SubtractionMatrix(4, 5);
        S.Subtraction();
        S.print();
        System.out.println("=======+============+============+=========");
        System.out.println("Subtraction of A,B :");
        SubtractionMatrix S1 = new SubtractionMatrix(4, 5);
        S1.Subtraction();
        S1.print();
        System.out.println("=======+============+============+=========");
    }
}

Output of this program is given bellow :

run:
Subtraction of A,B :
15 97 77 87 23
37 70 8 30 92
64 41 89 82 15
65 97 71 50 16
=======+============+============+=========
Subtraction of A,B :
18 59 18 25 41
36 44 39 73 65
88 29 43 19 40
26 82 53 62 42
=======+============+============+=========
BUILD SUCCESSFUL (total time: 1 second)

About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .