This is a simple Java program that will multiply two matrices. Follow the steps and don't miss a
- Nelson John
- Nov 24, 2017
- 1 min read

import java.util.Scanner;
public class JavaProgram
{
public static void main(String args[])
{
int m, n, p, q, sum = 0, c, d, k;
Scanner in = new Scanner(System.in);
System.out.print("Enter Number of Rows and Columns of First Matrix : ");
m = in.nextInt();
n = in.nextInt();
int first[][] = new int[m][n];
System.out.print("Enter First Matrix Elements : ");
for(c=0 ; c<m; c++)
{
for(d=0; d<n; d++)
{
first[c][d] = in.nextInt();
}
}
System.out.print("Enter Number of Rows and Columns of Second Matrix : ");
p = in.nextInt();
q = in.nextInt();
if ( n != p )
{
System.out.print("Matrix of the entered order can't be Multiplied..!!");
}
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
System.out.print("Enter Second Matrix Elements :\n");
for(c=0; c<p; c++)
{
for(d=0; d<q; d++)
{
second[c][d] = in.nextInt();
}
}
System.out.print("Multiplying both Matrix...\n");
for(c=0; c<m; c++)
{
for(d=0; d<q; d++)
{
for(k=0; k<p; k++)
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
System.out.print("Multiplication Successfully performed..!!\n"); System.out.print(" New Matrix is:\n");
for(c=0; c<m; c++)
{
for(d=0; d<q; d++)
{
System.out.print(multiply[c][d] + "\t");
}
System.out.print("\n");
}
}
}
}
This is what the output of the above program will look like...
Enter Number of Rows and Columns of Second Matrix : 3
3
Enter First Matrix Elements : 1
2
3
4
5
6
7
8
9
Enter Number of Rows and Columns of Second Matrix : 3
3
Enter Second Matrix Elements :9
8
7
6
5
4
3
2
1
Multiplying both matrix...
Multiplication Successfully performed..!!
New Matrix is :
30 24 18
84 69 54
128 114 90
Facebook: ayamnelrodinho1@gmail.com
Yahoo: talk2nelsonjohn2dat@yahoo.com
Whatsapp: +2349034595610
PLEASE KINDLY SHARE WITH FELLOW PROGRAMMERS AND ALSO SUBSCRIBE TO MY BLOG FOR MORE FEEDS.
Kommentare