Skip to content

Added code for Matrix Addition #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Programs/matrix/MatrixAddition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
public class MatrixAddition {
public static void main(String[] args) {
int[][] matrixA = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

int[][] matrixB = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};

int rows = matrixA.length;
int cols = matrixA[0].length;

int[][] resultMatrix = new int[rows][cols];

// Perform matrix addition
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
resultMatrix[i][j] = matrixA[i][j] + matrixB[i][j];
}
}

// Display the result
System.out.println("Matrix A:");
printMatrix(matrixA);

System.out.println("\nMatrix B:");
printMatrix(matrixB);

System.out.println("\nMatrix A + Matrix B:");
printMatrix(resultMatrix);
}

public static void printMatrix(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;

for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ It is very easy to contribute, you may follow these steps -
99.[RotateLinkedList](https://github.com/PrajaktaSathe/Java/blob/main/Programs/RotateLinkedList.java)-Program to demo rotating a linked list
100. [ReverseString](https://github.com/PrajaktaSathe/Java/blob/main/ReverseString.java) -Program to reverse a String using the java method substring.
101.[Overriding](https://github.com/PrajaktaSathe/Java/blob/main/Programs/Overriding.java)-Program to demo overriding in java
102. [MatrixAddition](https://github.com/PrajaktaSathe/Java/blob/main/Programs/matrix/MatrixAddition.java) - Perform element-wise addition between two matrices of the same dimensions.

# Contributors -
## A big thank you to all our contributors!!!
Expand Down