Archive

Posts Tagged ‘Add Matrix Program’

Java Program to add two matrices – Q8

January 4, 2009 Leave a comment

Q8: Java Program to add two matrices

… from College notes (BCA/MCA assignments):

class matrix_add{
	public static void main(String args[]){
		int mat1[][] = { {1,2,3},
						 {4,5,6},
						 {7,8,9} };
		int mat2[][] = { {9,8,7},
						 {6,5,4},
						 {3,2,1} };
		
		int tot[][] = new int[3][3];
		int i, j;
		
		for(i=0; i<3; i++){
			for(j=0; j<3; j++){
				tot[i][j] = mat1[i][j] + mat2[i][j];
			}
		}
		for(i=0; i<3; i++){
			for(j=0; j<3; j++){
				System.out.print(" " + tot[i][j]);
			}
			System.out.println();
		}
	}
}