Home » Programming » Java Programming » Java program source code for multiplying 2 Matrix

2 years ago (Jul 17, 2015) 2,247 views

Java program source code for multiplying 2 Matrix

Category: Java Programming, Programming Tags: , , , , by

import java.util.*;

class matrix
{
static Scanner input=new Scanner(System.in);
public static void main(String[] args)
{

//starting matrix programm…
//a matrix example
//a=|2 3| b=|5 6|
// |7 8| |3 2|

//result |2*5+3*3 2*6+3*2|
// |7*5+8*3 7*6+8*2|

//now we need to first input value of row and column
//we need to define row as r and collum as c

//we r now trying to take input by using foor loop
int r,c,k,addMatrix=0;
int ar,ac;

System.out.println(“Please input the row number of matrix a”);
ar=input.nextInt();
System.out.println(“Please input the colum number of matrix a”);
ac=input.nextInt();

int br,bc;

System.out.println(“Please input the row number of matrix b”);
br=input.nextInt();
System.out.println(“Please input the column number of matrix a”);
bc=input.nextInt();

int[][] a=new int[ar][ac];
int[][] b=new int[br][bc];
int[][] result=new int[ar][bc];

while(ac!=br){
System.out.println(“First matrix column and second matrix row need to be eqaul”);
//again taking input from user
System.out.println(“Please input the row number of matrix a again”);
ar=input.nextInt();
System.out.println(“Please input the colum number of matrix a again”);
ac=input.nextInt();

System.out.println(“Please input the row number of matrix b again”);
br=input.nextInt();
System.out.println(“Please input the column number of matrix a again”);
bc=input.nextInt();
}

if(ac==br){
//Input value of “a” matrix
System.out.println(“I nput value of a … u need to input value of 1st row first then second row”);
for(r=0;r<ar;r++){
for(c=0;c<ac;c++){
a[r][c]=input.nextInt();
}
}

for (r = 0; r < ar; r++) {
for (c = 0; c < ac; c++) {

System.out.print(a[r][c]+”\t”);
}
System.out.println(“\n”);
}
//input value of “b” matrix
System.out.println(“I nput value of b … u need to input value of row first”);

for(r=0;r<br;r++){
for(c=0;c<bc;c++){
b[r][c]=input.nextInt();
}
}

for (r = 0; r <br; r++) {

for (c = 0; c <bc; c++) {
System.out.print(b[r][c]+”\t”);
}
System.out.println(“\n”);
}

//multiplication value of matrix
for (r = 0; r <ar; r++) {
for (c = 0; c <bc; c++) {
for (k = 0; k <br; k++) {
addMatrix=addMatrix+a[r][k]*b[k][c];
} //end of k loop
result[r][c]=addMatrix;
addMatrix=0;
}
}

for (r = 0; r < ar; r++) {
for (c = 0; c <bc; c++) {
System.out.print(result[r][c]+”\t”);
}
System.out.println(“\n”);
}

}
}
}

 
You can only multiply 2 matrix by using this program :)
[As this is only source code of the pprogram you need a java compiler to run this program :) ]

About Post: 134

Love to play with programs!! :D

31 Responses to “Java program source code for multiplying 2 Matrix”

  1. click here says:

    … [Trackback]

    […] Read More: trickbd.co/java-program-source-code-for-multiplying-2-matrix/4197 […]

  2. … [Trackback]

    […] Read More: trickbd.co/java-program-source-code-for-multiplying-2-matrix/4197 […]

  3. sekis izle says:

    … [Trackback]

    […] Informations on that Topic: trickbd.co/java-program-source-code-for-multiplying-2-matrix/4197 […]

  4. … [Trackback]

    […] Read More here: trickbd.co/java-program-source-code-for-multiplying-2-matrix/4197 […]

  5. … [Trackback]

    […] Read More: trickbd.co/java-program-source-code-for-multiplying-2-matrix/4197 […]

  6. … [Trackback]

    […] Find More Informations here: trickbd.co/java-program-source-code-for-multiplying-2-matrix/4197 […]

  7. anami sikin says:

    … [Trackback]

    […] Informations on that Topic: trickbd.co/java-program-source-code-for-multiplying-2-matrix/4197 […]

  8. … [Trackback]

    […] Informations on that Topic: trickbd.co/java-program-source-code-for-multiplying-2-matrix/4197 […]

  9. … [Trackback]

    […] Read More here: trickbd.co/java-program-source-code-for-multiplying-2-matrix/4197 […]

  10. … [Trackback]

    […] There you will find 41426 more Infos: trickbd.co/java-program-source-code-for-multiplying-2-matrix/4197 […]

  11. … [Trackback]

    […] There you will find 24852 more Infos: trickbd.co/java-program-source-code-for-multiplying-2-matrix/4197 […]

  12. … [Trackback]

    […] Read More here: trickbd.co/java-program-source-code-for-multiplying-2-matrix/4197 […]

  13. … [Trackback]

    […] Informations on that Topic: trickbd.co/java-program-source-code-for-multiplying-2-matrix/4197 […]

  14. … [Trackback]

    […] Read More Infos here: trickbd.co/java-program-source-code-for-multiplying-2-matrix/4197 […]

  15. … [Trackback]

    […] Read More here: trickbd.co/java-program-source-code-for-multiplying-2-matrix/4197 […]

  16. great work says:

    … [Trackback]

    […] Read More Infos here: trickbd.co/java-program-source-code-for-multiplying-2-matrix/4197 […]

  17. … [Trackback]

    […] Find More Informations here: trickbd.co/java-program-source-code-for-multiplying-2-matrix/4197 […]

  18. … [Trackback]

    […] Read More: trickbd.co/java-program-source-code-for-multiplying-2-matrix/4197 […]

  19. … [Trackback]

    […] Read More Infos here: trickbd.co/java-program-source-code-for-multiplying-2-matrix/4197 […]

Leave a Reply

You must be logged in to post a comment.