If we want to add two matrices we can serve this purpose using this program.Matrix addition is a complex task and Java Program to add two matrices is most frequently asked interview and coding exam question,therefore its necessary to understand logic to implement this program.
In this topic we will take two examples. In Example-1 we will be take Static Inputs and Example-2 we will take User defined Inputs.
Matrix-1= | 1 2 3 | Matrix-2= | 1 2 3 | | 4 5 6 | | 4 5 6 | | 7 8 9 | | 7 8 9 | Matrix-3 = | 1+1 2+2 3+3 | | 2 4 6 | Matrix 1+2 | 4+4 5+5 6+6 | = | 8 10 12 | | 7+7 8+8 9+9 | | 14 16 18 |
From the above program it is very clear what we have to do in this program.
Example-1 Using Static Inputs
Algorithm/Program Explanation
- Start
- Initialize 3- 3X3 Matrix
- int matrix1[][]={{1,2,3},{4,5,6},{7,8,9}};
- int matrix2[][]={{1,2,3},{4,5,6},{7,8,9}};
- int matrix3[][]=new int[3][3];
- Define two loop
- for(int i=0;i<3;i++)
- for(int j=0;j<3;j++)
- condition-matrix3[i][j]=matrix1[i][j]+ matrix2[i][j]; //for subtraction change + sign with – sign
- Print matrix3
- End
Note: For Subtraction just change + with – Sign
/*
Java Program to Add/sub Two Matrices */
import java.util.*;
public class MatrixAddition{
public static void main(String args[]){
int matrix1[][]={{1,2,3},{4,5,6},{7,8,9}};
int matrix2[][]={{1,2,3},{4,5,6},{7,8,9}};
int matrix3[][]=new int[3][3];
System.out.println( " Matrix After Addition: ");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
matrix3[i][j]=matrix1[i][j]+ matrix2[i][j];//for subtraction change + sign with + sign
System.out.print(matrix3[i][j]+ " " );
}
System.out.println();
}
}
}
Output:
Matrix After Addition:
2 4 6
8 10 12
14 16 18
Example 2- Taking User Defined Inputs
/*
Java Program to Add Two Matrices */
import java.util.*;
public class MatrixAddition{
public static void main(String args[]){
int matrix1[][]=new int [3][3];
int matrix2[][]=new int[3][3];
int matrix3[][]=new int[3][3];
Scanner sc=new Scanner(System.in);
System.out.println("Enter Input for Matrix-1 3X3");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
matrix1[i][j]=sc.nextInt();
}
}
System.out.println("Enter Input for Matrix-2 3X3");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
matrix2[i][j]=sc.nextInt();
}
}
System.out.println( " Matrix After Addition: ");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
matrix3[i][j]=matrix1[i][j]+ matrix2[i][j];// for subtraction just change + sign with - sign
System.out.print(matrix3[i][j]+ " " );
}
System.out.println();
}
}
}
Output:
Enter Input for Matrix-1 3X3
1 2 3
4 5 6
7 8 9
Enter Input for Matrix-2 3X3
1 2 3
4 5 6
7 8 9
Matrix After Addition:
2 4 6
8 10 12
14 16 18
Related Topics:
- Arithmetic Operations Java ProgramArithmetic Operations Java Program There are several ways to code for this program; below are a few ways:- First Way:– …
- Java program to perform Logical and Relational operationsRelation operators are used to compare two quantities depending on their relation and depending upon relation it takes certain decision. logical operators are used when we want to form compound conditions by combing two or more relations
- Java program to Implement Mathematical functions defined in Java Math classMax(parameters) Method- Return the maximum value Static int max(int x, int y) Static long max(int x, int y) Static double …
Read moreJava program to Implement Mathematical functions defined in Java Math class
- Java basic Programs with ExplanationList of Basic Java Programs with Explanation|Min,Max Calculation|Factorial|Percentage Calculator|Fibonacci|Etc…..
- Java Program to find Min,Max Element in an ArrayJava Program to Find Minimum Element from an Array In this Java Program we are going to find minimum /smallest …
- Java Program to sort Array Elements in ascending and descending orderWrite a Java Program to sort Array Elements in Ascending order In this we will be taking user defined array …
Read moreJava Program to sort Array Elements in ascending and descending order
- Java Program to display Array item at Even & Odd positionProgram to display Array item at Even Position. In this program we will be taking user defined array, after taking …
Read moreJava Program to display Array item at Even & Odd position
- Program to Calculate Sum and Average of Array elementsIn this section, we will see various variations on how to write this program.In Example 1, we will be taking …
Read moreProgram to Calculate Sum and Average of Array elements
- Java Program to find Index of specific Element in an arrayJava Program to Find Index of Given Element in an array,Understand programming logic to implement this code,Also understand variation of writing this code.
- Program to find common elements between two arraysJava Program To find Common Elements Between two Integer Arrays,Also Understand the program logic to implement this code,also understand variations of code.
- Java Program to Add/Insert specific Element to an ArrayJava Program to Add/Insert Element at Specific Position in an Array,Understand Logic to implement this program|Two Variation are Explained in this post |Click here
- Java Program to delete specific element from an ArrayJava Program To Delete Element from an array,Also Understand the Logic to implement this code|Understand variations of writing this program|Click here
- Program to calculate frequency of each element in ArrayJava Program to Count Frequency of occurrence of each Element in the array,Also Understand the logic to implement this code
- Java Program to find Even and Odd Numbers in the arraysJava Program to Find Even and Odd Numbers in an Arrays,Also understand the logic to implement this code|Click here to know more..
- Java Program to search Given Number in an ArrayJava Program To Search Number in the Array, Also Understand the logic to implement this code, Also Understand variation of writing this code|Click here