Find The Multiplication Of Two Multidimensional Matrix by using Java Program
Code:
import java.util.*;
public class MatrixMulti
{
public static void main(String[] args)
{
int i,j,k,m,n,o,p,multipli=0;
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter The Number Of Rows Of 1st Matrix : ");
m = sc.nextInt();
System.out.print("\nEnter The Number Of Colums Of 1st Matrix : ");
n = sc.nextInt();
System.out.print("\nEnter The Number Of Rows Of 2nd Matrix : ");
o = sc.nextInt();
System.out.print("\nEnter The Number Of Colums Of 2nd Matrix : ");
p = sc.nextInt();
System.out.println();
if(n != o)
{
System.out.println("In This Order Matrix Can Not Be Multiplied With Each Other\n");
}
else
{
int first[][] = new int[m][n];
int second[][] = new int[o][p];
int multi[][] = new int[m][p];
System.out.println("Enter The 1st Matrix : \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
first[i][j]=sc.nextInt();
}
}
System.out.println("\nEnter The 2nd Matrix : \n");
for(i=0;i<o;i++)
{
for(j=0;j<p;j++)
{
second[i][j]=sc.nextInt();
}
}
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
for(k=0;k<o;k++)
{
multipli=multipli+(first[i][k]*second[k][j]);
}
multi[i][j]=multipli;
multipli=0;
}
}
System.out.println("\nMultiplication Of The Matrix : \n");
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
System.out.print(multi[i][j]+"\t");
}
System.out.println();
}
System.out.println();
}
}
}
Output of program:
import java.util.*;
public class MatrixMulti
{
public static void main(String[] args)
{
int i,j,k,m,n,o,p,multipli=0;
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter The Number Of Rows Of 1st Matrix : ");
m = sc.nextInt();
System.out.print("\nEnter The Number Of Colums Of 1st Matrix : ");
n = sc.nextInt();
System.out.print("\nEnter The Number Of Rows Of 2nd Matrix : ");
o = sc.nextInt();
System.out.print("\nEnter The Number Of Colums Of 2nd Matrix : ");
p = sc.nextInt();
System.out.println();
if(n != o)
{
System.out.println("In This Order Matrix Can Not Be Multiplied With Each Other\n");
}
else
{
int first[][] = new int[m][n];
int second[][] = new int[o][p];
int multi[][] = new int[m][p];
System.out.println("Enter The 1st Matrix : \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
first[i][j]=sc.nextInt();
}
}
System.out.println("\nEnter The 2nd Matrix : \n");
for(i=0;i<o;i++)
{
for(j=0;j<p;j++)
{
second[i][j]=sc.nextInt();
}
}
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
for(k=0;k<o;k++)
{
multipli=multipli+(first[i][k]*second[k][j]);
}
multi[i][j]=multipli;
multipli=0;
}
}
System.out.println("\nMultiplication Of The Matrix : \n");
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
System.out.print(multi[i][j]+"\t");
}
System.out.println();
}
System.out.println();
}
}
}
Output of program:
No comments