Sun Sep 12 2021

Matrix Addition

File Name: matrix-addition.java

import java.io.*;

class addmatrix {

	/* Constructor */
	addmatrix(int a[ ][ ], int b[ ][ ]) {
		int ans[ ][ ] = new int[3][3];
		System.out.println("Matrix Addition");
		for(int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				ans[i][j]= a[i][j] + b[i][j];
				System.out.print(ans[i][j]+"\t");
			}
		}
	}
}

class matrix {
	public static void main(String args[ ]) {
		int a[ ][ ] = {{1,2,3},{4,5,6},{7,8,9}};
		int b[ ][ ] = {{4,4,4},{4,4,4},{4,4,4}};
		addmatrix addmat = new addmatrix(a, b);
	}
}



/* Output */
Matrix Addition
5 6 7
8 9 10
11 12 13
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.