Google Search

Monday, October 10, 2011

Write a Java program to multiply two given matrices.



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<m;i++)
{
for(j=0;j<n;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<p;i++)
{
for( j=0;j<q;j++)
{
b[i][j]=Integer.parseInt(br.readLine());
}
}


}
int c[][]=new int[m][i];
if(n==p)
{
for( i=0;i<m;i++)
{
for( j=0;j<q;j++)
{
c[i][j]=0;
for( k=0;k<p;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
System.out.println(c[i][j]);
}
}
}
}
} } .

5 comments:

DEWHURST TOULSON said...

The purpose: to help break the isolation and boost the morale of detainees, who may be held for months before being released or deported.
marketing dissertation topics

MANTU KUMAR said...
This comment has been removed by the author.
MANTU KUMAR said...
This comment has been removed by the author.
MANTU KUMAR said...
This comment has been removed by the author.
Sangita Kumari said...

Its a very useful post. Please take care of the indentation it would make the program look good and cleaner.
Check this out : Best Way To Learn Java Programming

Post a Comment