0% found this document useful (0 votes)
8 views2 pages

Matrix

Uploaded by

joshishridhar186
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Matrix

Uploaded by

joshishridhar186
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Scanner;
public class AdditonOfTwoMatrix
{
public static void main(String[] args)
{
Scanner scanner = new
Scanner(System.in);
System.out.print("Enter the number of rows in the matrices: ");
int rows = scanner.nextInt();
System.out.print("Enter the number of columns in the matrices: ");
int columns = scanner.nextInt();
int[][] matrix1 = new int[rows] [columns];
int[][] matrix2 = new int[rows] [columns];
int[][] resultMatrix = new int[rows] [columns];
System.out.println("Enter elements first matrix:");
for (int i = 0 ; i < rows; i++)
{
for (int j = 0 ; j < columns; j++)
{
System.out.print("Element [" + i + "][" + j + "]: ");
matrixl[i][j] = scanner.nextInt();
}
}

System.out.println("Enter elements of the second matrix:");


for (int i = 0 ; i < rows; i++)
{
for (int j = 0 ; j < columns; j++)
{
System.out.print("Element [" + i + "][" + j + "]: ");
matrix2[i][j] = scanner.nextInt();
}
}

for (int i = 0 ; i < rows; i++)


{
for (int j = 0 ; j < columns; j++)
{
resultMatrix[i][j] = matrix1[i][j]+ matrix2[i][j];
}
}
System.out.println("Resultant Matrix after addition:");
for (int i = 0 ; i < rows; i++)
{
for (int j = 0 ; j < columns; j++)
{
System.out.print(resultMatrix[i][j]+ "\t")
System.out.println();
}
scanner.close();
}
}

OUTPUT:
Enter the number of rows in the matrices: 4
Enter the number of colums in the matrices: 3
Enter elements of the first matrix:
Element [0][0]: 1
Element [0][1]: 2
Element [0][2]: 3
Element [1][0]: 4
Element [1][1]: 66
Element [1][2]: 77
Element [2][0]: 9
Element [2][1]: 4
Element [2][2]: 6
Element [3][0]: 7
Element [3][1]: 8
Element [3][2]: 22

Enter elements of the second matrix.

Element [0][0]: 1
Element [0][1]: 4
Element [0][2]: 5
Element [1][0] 6
Element [1][1]: 77
Element [1][2]: 33
Element [2][0]: 99
Element [2][1]: 33
Element [2][2]: 55
Element [3][0]: 09
Element [3][1]: 99
Element [3][2]: 20

Resultant Matrix after addition:

2 6 8
10 143 110
108 37 61
96 187 42

You might also like