Program Statement: Write a JAVA program to compute Matrix Multiplication.
Tested
on:
Software: Windows 8, Jdk 1.6, Netbeans IDE
Hardware: Intel Core i3, 4 GB RAM, 500 GB HDD
Software: Windows 8, Jdk 1.6, Netbeans IDE
Hardware: Intel Core i3, 4 GB RAM, 500 GB HDD
Source
Code
@Author: Praveen
Kanwar
import java.util.*;
class Multiplication
{
int[][] mul(int x[][],int y[][],int z[][],int m,int n,int p,int q)
{
for(int r=0;r<m;r++)
{
for(int c=0;c<n;c++)
{
z[r][c]=0;
for(int k=0;k<p;k++)
z[r][c]=z[r][c]+x[r][k]*y[k][c];
}
}
return z;
}
}
class Matrixmul
{
public static void main(String[] args)
{
System.out.println("\n Enter the number or rows and columns in matrix A:\n");
Scanner sn=new Scanner(System.in);
int m=sn.nextInt();
int n=sn.nextInt();
int result[][]=new int[m][n];
int A[][]=new int[m][n];
System.out.println("Enter elements into matrix A:\n");
for(int r=0;r<m;r++)
for(int c=0;c<n;c++)
A[r][c]=sn.nextInt();
System.out.println("Enter the number or rows and columns in matrix B:\n");
int p=sn.nextInt();
int q=sn.nextInt();
int B[][]=new int[p][q];
System.out.println(" Enter elements into matrix B:\n");
for(int r=0;r<p;r++)
for(int c=0;c<q;c++)
B[r][c]=sn.nextInt();
Multiplication obj=new Multiplication();
if(n==p)
{
obj.mul(A,B,result,m,n,p,q);
for(int r=0;r<m;r++)
{
for(int c=0;c<n;c++)
{
System.out.print(result[r][c] +"\t");
}
System.out.print("\n");
}
}
else
System.out.println(" Matrix Multiplication Not Possible.");
}
}
import java.util.*;
class Multiplication
{
int[][] mul(int x[][],int y[][],int z[][],int m,int n,int p,int q)
{
for(int r=0;r<m;r++)
{
for(int c=0;c<n;c++)
{
z[r][c]=0;
for(int k=0;k<p;k++)
z[r][c]=z[r][c]+x[r][k]*y[k][c];
}
}
return z;
}
}
class Matrixmul
{
public static void main(String[] args)
{
System.out.println("\n Enter the number or rows and columns in matrix A:\n");
Scanner sn=new Scanner(System.in);
int m=sn.nextInt();
int n=sn.nextInt();
int result[][]=new int[m][n];
int A[][]=new int[m][n];
System.out.println("Enter elements into matrix A:\n");
for(int r=0;r<m;r++)
for(int c=0;c<n;c++)
A[r][c]=sn.nextInt();
System.out.println("Enter the number or rows and columns in matrix B:\n");
int p=sn.nextInt();
int q=sn.nextInt();
int B[][]=new int[p][q];
System.out.println(" Enter elements into matrix B:\n");
for(int r=0;r<p;r++)
for(int c=0;c<q;c++)
B[r][c]=sn.nextInt();
Multiplication obj=new Multiplication();
if(n==p)
{
obj.mul(A,B,result,m,n,p,q);
for(int r=0;r<m;r++)
{
for(int c=0;c<n;c++)
{
System.out.print(result[r][c] +"\t");
}
System.out.print("\n");
}
}
else
System.out.println(" Matrix Multiplication Not Possible.");
}
}
Output
Enter the number or rows and columns in matrix A:
3 3
Enter elements into matrix A:
1 2 3
4 5 6
7 8 9
Enter the number or rows and columns in matrix B:
3 3
Enter elements into matrix B:
1 2 3
6 7 8
4 5 6
3 3
Enter elements into matrix A:
1 2 3
4 5 6
7 8 9
Enter the number or rows and columns in matrix B:
3 3
Enter elements into matrix B:
1 2 3
6 7 8
4 5 6
25 31 37
58 73 88
91 115 139
Post a Comment