Advertisements
Home > Java > Java Program to add two matrices – Q8

Java Program to add two matrices – Q8


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();
		}
	}
}

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: