Multiplication of matrix



CODE:
import java.util.*;
class Product
{
     public static void main(String args[])
     {
           int m,n,p,i,j,k;
           Scanner sc = new Scanner(System.in);
           System.out.print("Enter no of rows & columns :");
           m=sc.nextInt();
           n=sc.nextInt();
           p=sc.nextInt();
           int a[][]=new int[m][n];
           int b[][]=new int[n][p];
           int c[][]=new int[m][p];
           System.out.print("Matrix A:");
           for(i=0;i<=m-1;i++)
           {
                for(j=0;j<=n-1;j++)
                {
                     System.out.print("enter a no:");
                     a[i][j]=sc.nextInt();
                }
           }
           System.out.print("Matrix B:");
           for(i=0;i<=n-1;i++)
           {
                for(j=0;j<=p-1;j++)
                {
                     System.out.print("enter a no:");
                     b[i][j]=sc.nextInt();
                }
           }
           for(i=0;i<=m-1;i++)
           {
                for(j=0;j<=p-1;j++)
                {
                     c[i][j]=0;
                     for(k=0;k<=n-1;k++)
                     {
                           c[i][j]+=a[i][k]*b[k][j];
                }
           }
           System.out.println("product Matrix");
           for(i=0;i<=m-1;i++)
           {
                for(j=0;j<=p-1;j++)
                {
                     System.out.print(c[i][j]+"\t");
                }
                System.out.println();
           }
     }
} 
OUTPUT:         
           





No comments :

Post a Comment