9/1/2021 Java Program to multiply 2 Matrices - Javatpoint
Home Java Programs OOPs String Exception Multithreading
https://www.javatpoint.com/java-program-to-multiply-two-matrices 1/6
9/1/2021 Java Program to multiply 2 Matrices - Javatpoint
Java Program to multiply two matrices
We can multiply two matrices in java using binary * operator and executing another loop. A matrix is
also known as array of arrays. We can add, subtract and multiply matrices.
In case of matrix multiplication, one row element of first matrix is multiplied by all columns of second
matrix.
Let's see a simple example to multiply two matrices of 3 rows and 3 columns.
public class MatrixMultiplicationExample{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
//creating another matrix to store the multiplication of two matrices
int c[][]=new int[3][3]; //3 rows and 3 columns
//multiplying and printing multiplication of 2 matrices
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
https://www.javatpoint.com/java-program-to-multiply-two-matrices 2/6
9/1/2021 Java Program to multiply 2 Matrices - Javatpoint
}//end of k loop
System.out.print(c[i][j]+" "); //printing matrix element
}//end of j loop
System.out.println();//new line
}
}}
Test it Now
Output:
6 6 6
12 12 12
18 18 18
4 Times National Award Winner
ThinkNEXT Technologies Open
← Prev Next →
https://www.javatpoint.com/java-program-to-multiply-two-matrices 3/6
9/1/2021 Java Program to multiply 2 Matrices - Javatpoint
For Videos Join Our Youtube Channel: Join Now
Feedback
Help Others, Please Share
Learn Latest Tutorials
digital elasticsearch entity Firewall tutorial
marketing tutorial tutorial framework
Firewall
tutorial
Digital Marketing Elasticsearch
Entity Framework
Functional Google Colab Graph Theory Groovy tutorial
Programming tutorial tutorial
Groovy
tutorial
Google Colab Graph Theory
Functional
Programming
Group Informatica Ionic tutorial ITIL tutorial
Discussion tutorial
Ionic ITIL
tutorial
Informatica
Group Discussion
https://www.javatpoint.com/java-program-to-multiply-two-matrices 4/6
9/1/2021 Java Program to multiply 2 Matrices - Javatpoint
IOS angular deep learning
Development material tutorial tutorial
tutorial
Angular Material Deep Learning
IOS with Swift
Preparation
Aptitude Logical Verbal Ability Interview
Reasoning Questions
Aptitude Verbal Ability
Reasoning Interview Questions
Company
Interview
Questions
Company Questions
Trending Technologies
Artificial AWS Tutorial Selenium Cloud
Intelligence tutorial Computing
AWS
Tutorial tutorial
Selenium
Artificial Cloud Computing
Intelligence
Hadoop tutorial ReactJS Data Science Angular 7
Tutorial Tutorial Tutorial
Hadoop
ReactJS Data Science Angular 7
Blockchain Git Tutorial Machine DevOps
Tutorial Learning Tutorial Tutorial
Git
Blockchain Machine Learning DevOps
B.Tech / MCA
https://www.javatpoint.com/java-program-to-multiply-two-matrices 5/6
9/1/2021 Java Program to multiply 2 Matrices - Javatpoint
DBMS tutorial Data Structures DAA tutorial Operating
tutorial System tutorial
DBMS DAA
Data Structures Operating System
Computer Compiler Computer Discrete
Network tutorial Design tutorial Organization and Mathematics
Architecture Tutorial
Computer Network Compiler Design
Computer Discrete
Organization Mathematics
Ethical Hacking Computer Software html tutorial
Tutorial Graphics Tutorial Engineering
Web Technology
Tutorial
Ethical Hacking Computer Graphics
Software
Engineering
Cyber Security Automata C Language C++ tutorial
tutorial Tutorial tutorial
C++
Cyber Security Automata C Programming
Java tutorial .Net Python tutorial List of
Framework Programs
Java Python
tutorial
Programs
.Net
Control Data Mining Data
Systems tutorial Tutorial Warehouse
Tutorial
Control System Data Mining
Data Warehouse
https://www.javatpoint.com/java-program-to-multiply-two-matrices 6/6