b) Write a Java
program to multiply two given matrices.
import java.lang.*;
import java.io.*;
class arraymul
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
system.out.print("enter no of rows");
int m=Integer.parseInt(br.readLine());
system.out.print("enter no of columns");
int n=Integer.parseInt(br.readLine());
int a[ ][ ]=new int[m][n];
int i,j,k;
for( i=0;i
{
for(j=0;j
{
a[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.print("enter no of
rows");
int p=Integer.parseInt(br.readLine());
System.out.print("enter no of
columns");
int q=Integer.parseInt(br.readLine());
int b[ ][ ]=new int[p][q];
{
for( i=0;i
{
for( j=0;j
{
b[i][j]=Integer.parseInt(br.readLine());
}
}
}
int c[][]=new int[m][i];
if(n==p)
{
for( i=0;i
{
for( j=0;j
{
c[i][j]=0;
for( k=0;k
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
for(i=0;i
{
for(j=0;j
{
System.out.println(c[i][j]);
}
}
}
}
} }
No comments:
Post a Comment