/* multiplication of two matrix is as follows :
c[i][j] = a[i][k] * b[k][j]
*/
import java.util.*;
class ArrayMulti
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int [][]ar1=new int[2][2];
int [][]ar2=new int[2][2];
int [][]ar3=new int[2][2];
int i,j,k;
//////first matrix
System.out.println("Enter the elements of the matrix :");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
ar1[i][j]=sc.nextInt();
}
}
System.out.println(" the elements of the matrix First are :");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.print(ar1[i][j]+" ");
}
System.out.println();
}
///////////// 2nd matrix
System.out.println("Enter the elements of the second matrix :");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
ar2[i][j]=sc.nextInt();
}
}
System.out.println(" the elements of the matrix second are :");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.print(ar2[i][j]+" ");
}
System.out.println();
}
///// multiplication
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
ar3[i][j]=0;
for(k=0;k<2;k++)
ar3[i][j]+=ar1[i][k]*ar2[k][j];
}
}
/// display
System.out.println("Multiplication of the matrix is : ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.print(ar3[i][j]+" ");
}
System.out.println();
}
}
}
Output
Enter the elements of the matrix :
4
1
2
4
the elements of the matrix First are :
4 1
2 4
Enter the elements of the second matrix :
3
2
5
7
the elements of the matrix second are :
3 2
5 7
Multiplication of the matrix is :
17 15
26 32
c[i][j] = a[i][k] * b[k][j]
*/
import java.util.*;
class ArrayMulti
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int [][]ar1=new int[2][2];
int [][]ar2=new int[2][2];
int [][]ar3=new int[2][2];
int i,j,k;
//////first matrix
System.out.println("Enter the elements of the matrix :");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
ar1[i][j]=sc.nextInt();
}
}
System.out.println(" the elements of the matrix First are :");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.print(ar1[i][j]+" ");
}
System.out.println();
}
///////////// 2nd matrix
System.out.println("Enter the elements of the second matrix :");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
ar2[i][j]=sc.nextInt();
}
}
System.out.println(" the elements of the matrix second are :");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.print(ar2[i][j]+" ");
}
System.out.println();
}
///// multiplication
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
ar3[i][j]=0;
for(k=0;k<2;k++)
ar3[i][j]+=ar1[i][k]*ar2[k][j];
}
}
/// display
System.out.println("Multiplication of the matrix is : ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.print(ar3[i][j]+" ");
}
System.out.println();
}
}
}
Output
Enter the elements of the matrix :
4
1
2
4
the elements of the matrix First are :
4 1
2 4
Enter the elements of the second matrix :
3
2
5
7
the elements of the matrix second are :
3 2
5 7
Multiplication of the matrix is :
17 15
26 32
No comments:
Post a Comment