Matrix Multiplication
Do you like this story?
import javax.swing.JOptionPane;
import java.io.*;
import java.math.*;
class Matrix1
{
public static void main(String[] args) throws IOException
{
int i,j,k;
String N1=JOptionPane.showInputDialog("enter first matrix rows");
String N2=JOptionPane.showInputDialog("enter first matrix columns");
String N3=JOptionPane.showInputDialog("enter second matrix rows");
String N4=JOptionPane.showInputDialog("enter second matrix columns");
int r1=Integer.parseInt(N1);
int c1=Integer.parseInt(N2);
int r2=Integer.parseInt(N3);
int c2=Integer.parseInt(N4);
if(c1==r2)
{
int a[][]=new int[r1][c1];
int b[][]=new int[r2][c2];
int c[][]=new int[r1][c2];
//System.out.println("enter the elements of matrix A");
JOptionPane.showMessageDialog(null,"enter A elements","message",JOptionPane.INFORMATION_MESSAGE);
for(i=0;i
for(j=0;j
{
String A=JOptionPane.showInputDialog(" ");
a[i][j]=Integer.parseInt(A);
}
//System.out.println("enter the elements of matrix B");
JOptionPane.showMessageDialog(null,"enter b elements","message",JOptionPane.INFORMATION_MESSAGE);
for(i=0;i
for(j=0;j
{
String B=JOptionPane.showInputDialog(" ");
b[i][j]=Integer.parseInt(B);
}
for(i=0;i
{
for(j=0;j
{
c[i][j]=0;
for(k=0;k
c[i][j]=a[i][k]*b[k][j]+c[i][j];
}
}
//System.out.println("elements of resultant matrix:");
JOptionPane.showMessageDialog(null,"RESULTANT MATRIX","message",JOptionPane.INFORMATION_MESSAGE);
for(i=0;i
{
for(j=0;j
{
System.out.print(c[i][j]+"\t");
}
System.out.println();
}
}
else
//System.out.println("Matrix multiplication is not possible");
JOptionPane.showMessageDialog(null,"MATRIXMULTIPLICATION IS","MESSAGE",JOptionPane.INFORMATION_MESSAGE);
//System.exit(0);
}
}
import java.io.*;
import java.math.*;
class Matrix1
{
public static void main(String[] args) throws IOException
{
int i,j,k;
String N1=JOptionPane.showInputDialog("enter first matrix rows");
String N2=JOptionPane.showInputDialog("enter first matrix columns");
String N3=JOptionPane.showInputDialog("enter second matrix rows");
String N4=JOptionPane.showInputDialog("enter second matrix columns");
int r1=Integer.parseInt(N1);
int c1=Integer.parseInt(N2);
int r2=Integer.parseInt(N3);
int c2=Integer.parseInt(N4);
if(c1==r2)
{
int a[][]=new int[r1][c1];
int b[][]=new int[r2][c2];
int c[][]=new int[r1][c2];
//System.out.println("enter the elements of matrix A");
JOptionPane.showMessageDialog(null,"enter A elements","message",JOptionPane.INFORMATION_MESSAGE);
for(i=0;i
for(j=0;j
{
String A=JOptionPane.showInputDialog(" ");
a[i][j]=Integer.parseInt(A);
}
//System.out.println("enter the elements of matrix B");
JOptionPane.showMessageDialog(null,"enter b elements","message",JOptionPane.INFORMATION_MESSAGE);
for(i=0;i
for(j=0;j
{
String B=JOptionPane.showInputDialog(" ");
b[i][j]=Integer.parseInt(B);
}
for(i=0;i
{
for(j=0;j
{
c[i][j]=0;
for(k=0;k
c[i][j]=a[i][k]*b[k][j]+c[i][j];
}
}
//System.out.println("elements of resultant matrix:");
JOptionPane.showMessageDialog(null,"RESULTANT MATRIX","message",JOptionPane.INFORMATION_MESSAGE);
for(i=0;i
{
for(j=0;j
{
System.out.print(c[i][j]+"\t");
}
System.out.println();
}
}
else
//System.out.println("Matrix multiplication is not possible");
JOptionPane.showMessageDialog(null,"MATRIXMULTIPLICATION IS","MESSAGE",JOptionPane.INFORMATION_MESSAGE);
//System.exit(0);
}
}

This post was written by: Rajendra Prasad
Rajendra Prasad is a professional blogger, web designer and front end web developer. Follow him on Facebook