Matrix Addition Example Java Program
Definition
A matrix (plural matrices) is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns that is treated in certain prescribed ways. Matrix addition is the operation of adding two matrices by adding the corresponding entries together. Two matrices must have an equal number of rows and columns to be added. The sum of two matrices A and B will be a matrix which has the same number of rows and columns as do A and B. The sum of A and B, denoted A + B, is computed by adding corresponding elements of A and B.Matrix Addition Example Program
import java.util.Scanner;
class MatrixAddition{
public static void main(String args[]){
int rows,columns, c, d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix");
rows= in.nextInt();
columns = in.nextInt();
int matrix1[][] = new int[rows][columns];
int matrix2[][] = new int[rows][columns];
int sum[][] = new int[rows][columns];
System.out.println("Enter the elements of matrix1");
for ( c = 0 ; c < rows ; c++ ){
for ( d = 0 ; d < columns ; d++ ){
matrix1[c][d] = in.nextInt();
}
}
System.out.println("Enter the elements of matrix2");
for ( c = 0 ; c < rows ; c++ ){
for ( d = 0 ; d < columns ; d++ ){
matrix2[c][d] = in.nextInt();
}
}
for ( c = 0 ; c < rows ; c++ ){
for ( d = 0 ; d < columns ; d++ ){
sum[c][d] = matrix1[c][d] + matrix2[c][d];
}
}
System.out.println("Sum of entered matrices:");
for ( c = 0 ; c < rows ; c++ ){
for ( d = 0 ; d < columns ; d++ ){
System.out.print(sum[c][d]+"\t");
}
System.out.println();
}
}
}
Sample Output
Output is: Enter the number of rows and columns of matrix 3 3 Enter the elements of matrix1 5 8 3 9 2 5 9 4 3 Enter the elements of matrix2 7 0 2 4 7 8 6 5 1 Sum of entered matrices: 12 8 5 13 9 13 15 9 4
Calculation Programs
- Area Of Circle Example Java Program
- Odd Or Even Example Java Program
- Swap With Variable Example Java Program
- Swap Without Variable Example Java Program
- Leap Year Or Not Example Java Program
- Factorial Example Java Program
- LCM Example Java Program
- GCD Example Java Program
- Simple Calculator Example Java Program
- Multiplication Table Example Java Program
- Palindrome Number Example Java Program
- Area Of Rectangle Example Java Program
- Palindrome String Example Java Program
- Check whether the given number is Armstrong number or not Example Java Program
- Armstrong Number In A Particular Range Example Java Program
- Create Matrix Example Java Program
- Matrix Addition Example Java Program
- Matrix Subtraction Example Java Program
- Matrix Multiplication Example Java Program
- Transpose Of A Matrix Example Java Program
- Square Root Example Java Program
- Area Of Square Example Java Program
- Square Root Without InBuilt Functions Example Java Program
- Twin Prime Example Java Program
- Circumference Of Circle Example Java Program
- Sum Of Three Numbers Example Java Program
- Largest Of Three Numbers Example Java Program
- Smallest Number Example Java Program
- Average of n numbers Example Java Program
- Prime Number Example Java Program
Read More Articles
- Multiple Inheritance Using Interface Example Java Program
- Single Inheritance Example Java Program
- Multilevel Inheritance Example Java Program
- Hierarchical Inheritance Example Java Program
- Create Matrix Example Java Program
- Find all Substrings of a given string Example Java Program
- Sum Of Three Numbers Example Java Program
- Twin Prime Example Java Program
- Heap Sort Example Java Program
- Compile Time Polymorphism Example Java Program
