Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package problem.pkg11;
- import java.util.Scanner;
- public class Problem11 {
- public static void main(String[] args) {
- Scanner scan=new Scanner(System.in);
- //Multiplication of two square matrix
- //int n=scan.nextInt();
- int m1=scan.nextInt();
- int n1=scan.nextInt();
- int a[][]=new int [m1+1][n1+1];
- for(int i=1;i<=m1;i++){
- for(int j=1;j<=n1;j++){
- a[i][j]=scan.nextInt();
- }
- }
- int m2=scan.nextInt();
- int n2=scan.nextInt();
- int b[][]=new int [m2+1][n2+1];
- for(int i=1;i<=m2;i++){
- for(int j=1;j<=n2;j++){
- b[i][j]=scan.nextInt();
- }
- }
- if(n1!=m2){
- System.out.println("Multiplication not possible");
- }
- else{
- int c[][]=new int[m1+1][n2+1];
- int sum;
- for(int i=1;i<=m1;i++){
- for(int j=1;j<=n2;j++){
- sum=0;
- for(int k=1;k<=m2;k++){
- sum+=(a[j][k]*b[k][j]);
- }
- c[i][j]=sum;
- }
- }
- for(int i=1;i<=m1;i++){
- for(int j=1;j<=n2;j++){
- System.out.print(c[i][j]+" ");
- }
- System.out.printf("\n");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment