Matrix Multiplication

import java.util.*;
class matrixmultiply
{
public static void main(String[]args)throws Exception
{
Scanner d=new Scanner(System.in);
int m,n,p,q,i,j,k,s=0;
System.out.println("enter m & n");
m=d.nextInt();
n=d.nextInt();
System.out.println("enter p & q");
p=d.nextInt();
q=d.nextInt();
int a[][]=new int[m][n];
int b[][]=new int[p][q];
int c[][]=new int[m][q];

System.out.println("Enter the elements of a");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)

a[i][j]=d.nextInt();
}
}

System.out.println("Enter the elements of b");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
b[i][j]=d.nextInt();
}
}

for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<p;k++)
{
s+=a[i][k]*b[k][j];
}
c[i][j]=s;
s=0;
}
}

for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}

No comments:

Post a Comment

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...