fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Matrixmultiplication
  9. {
  10. public static void main(String[] args)
  11. {
  12. Scanner scan = new Scanner(System.in);
  13. int [][] a,b,c;
  14. int size;
  15. System.out.print("Enter the Size of the matrix :");
  16. size = scan.nextInt();
  17. a=new int[size][size];
  18. b=new int[size][size];
  19. c=new int[size][size];
  20. System.out.println("Enter the elements of the First matrix");
  21. for(int i=0;i<size;i++)
  22. {
  23. for(int j=0;j<size;j++)
  24. {
  25. System.out.print("Enter the element a[" + i +"]["+ j + "] : ");
  26. a[i][j] = scan.nextInt();
  27. }
  28. }
  29. System.out.println("Enter the elements of the Second matrix");
  30. for(int i=0;i<size;i++)
  31. {
  32. for(int j=0;j<size;j++)
  33. {
  34. System.out.print("Enter the element b[" + i +"]["+ j + "] : ");
  35. b[i][j] = scan.nextInt();
  36. }
  37. }
  38.  
  39. System.out.println("The Product of the two matrix is : ");
  40. for(int i=0;i<size;i++)
  41. {
  42. for(int j=0;j<size;j++)
  43. {
  44. int sum = 0;
  45. for(int k=0;k<size;k++)
  46. {
  47. sum +=(a[i][k] * b[k][j]);
  48. }
  49. c[i][j] = sum;
  50. System.out.print(c[i][j] + "\t");
  51. }
  52. System.out.println();
  53. }
  54.  
  55. }
  56.  
  57. }
Success #stdin #stdout 0.06s 4386816KB
stdin
2

1 1
1 1

1 1
1 1
stdout
Enter the Size of the matrix :Enter the elements of the First matrix
Enter the element a[0][0] : Enter the element a[0][1] : Enter the element a[1][0] : Enter the element a[1][1] : Enter the elements of the Second matrix
Enter the element b[0][0] : Enter the element b[0][1] : Enter the element b[1][0] : Enter the element b[1][1] : The Product of the two matrix is : 
2	2	
2	2