This program can be used to do matrix multiplication in java. Click on Read More...
Code
import java.util.Scanner; /** * * @author Devansh Dhrafani * */ public class MatrixMultiplication { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner myScanner=new Scanner(System.in); int a[][]=new int[100][100]; int b[][]=new int[100][100]; int c[][]=new int[100][100]; System.out.println("Multiplication of 2 Matrices"); System.out.println("Enter the number of Rows and Columns of Matrix-A (max. 100)"); System.out.print("Rows = "); int m=myScanner.nextInt(); System.out.print("Columns = "); int n=myScanner.nextInt(); System.out.println("Enter the number of Rows and Columns of Matrix-B (max. 100)"); System.out.print("Rows = "); int p=myScanner.nextInt(); System.out.print("Columns = "); int q=myScanner.nextInt(); if (n==p) { System.out.println("Matrices can be multiplied.. !!"); System.out.println("Input Matrix-A"); for (int i=0;i<m;i++) { for (int j=0;j<n;j++) { a[i][j]=myScanner.nextInt(); } } System.out.println("Input Matrix-B"); for (int i=0;i<p;i++) { for (int j=0;j<q;j++) { b[i][j]=myScanner.nextInt(); } } System.out.println("Matrix-A :"); for (int i=0;i<m;i++) { System.out.println(); for (int j=0;j<n;j++) { System.out.print(a[i][j] + "\t"); } } System.out.println(); System.out.println("Matrix-B :"); for (int i=0;i<p;i++) { System.out.println(); for (int j=0;j<q;j++) { System.out.print(b[i][j] + "\t"); } } for (int i=0;i<m;i++) for (int j=0;j<q;j++) { c[i][j]=0; for (int k=0;k<n;k++) c[i][j]+=a[i][k]*b[k][j]; } System.out.println(); System.out.println("The product of 2 Matrices is :"); for (int i=0;i<p;i++) { System.out.println(); for (int j=0;j<q;j++) { System.out.print(c[i][j] + "\t"); } } System.out.println(); } else { System.out.println("Whoa.. !!"); System.out.println("Matrices are Incompatible for Multiplication."); } } }
Output
Link for Download
http://goo.gl/6tLXMW
This is really helpful, I have shared this idea to my friends too. Getting ideas from someone else is really great. We can do something.
ReplyDeleteredirect here