Advertisements
Home > Java > Java Program to multiply two matrices – Q9

Java Program to multiply two matrices – Q9


Q9: Java Program to multiply two matrices

class matrix_mul{
	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, k;

		for(i=0; i<3; i++){
			for(j=0; j<3; j++){
				tot[i][j] = 0;
				for(k=0; k<3; k++){
					tot[i][j] = tot[i][j] + (mat1[i][k] * mat2[k][j]);
				}
			}
		}

		for(i=0; i<3; i++){
			for(j=0; j<3; j++){
				System.out.print(" " + tot[i][j]);
			}
			System.out.println();
		}
	}
}

… from College notes (BCA/MCA assignments):


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: