Python/C/C++/JAVA

Moderate Practice Programs with Code and Concept

By D.S

Addition and Subtraction of two Matrices

When it comes to adding or subtracting matrices using programming languages like C, C++, Python, or Java, the process is pretty straightforward. The addition and subtraction of two matrices require each element in the first matrix to be added or subtracted from the corresponding element in the second matrix. In other words, if we have two m x n matrices A and B, to add them together we simply iterate through each row i and column j and calculate Cij = Aij + Bij where C is a new matrix. To subtract them, the calculation is done in a similar way as Cij = Aij - Bij. These operations can be easily implemented in any programming language using nested loops that traverse every row and column index of both matrices. Overall, the addition and subtraction of matrices are fundamental mathematical operations that can be performed efficiently with any of these languages.

(a.) C Code

#include <stdio.h>

int main() {
  int matrix1[2][2] = {{1, 2}, {3, 4}};
  int matrix2[2][2] = {{5, 6}, {7, 8}};
  int result[2][2];

  // Addition
  printf("Addition Result:
");
  for (int i = 0; i < 2; ++i) {
      for (int j = 0; j < 2; ++j) {
          result[i][j] = matrix1[i][j] + matrix2[i][j];
          printf("%d ", result[i][j]);
      }
      printf("
");
  }

  // Subtraction
  printf("Subtraction Result:
");
  for (int i = 0; i < 2; ++i) {
      for (int j = 0; j < 2; ++j) {
          result[i][j] = matrix1[i][j] - matrix2[i][j];
          printf("%d ", result[i][j]);
      }
      printf("
");
  }

  return 0;
}
Output:-
Addition Result:
6 8
10 12
Subtraction Result:
-4 -4
-4 -4

(b.) C++ Code

#include <iostream>
using namespace std;

int main() {
  int matrix1[2][2] = {{1, 2}, {3, 4}};
  int matrix2[2][2] = {{5, 6}, {7, 8}};
  int result[2][2];

  // Addition
  cout << "Addition Result:" << endl;
  for (int i = 0; i < 2; ++i) {
      for (int j = 0; j < 2; ++j) {
          result[i][j] = matrix1[i][j] + matrix2[i][j];
          cout << result[i][j] << " ";
      }
      cout << endl;
  }

  // Subtraction
  cout << "Subtraction Result:" << endl;
  for (int i = 0; i < 2; ++i) {
      for (int j = 0; j < 2; ++j) {
          result[i][j] = matrix1[i][j] - matrix2[i][j];
          cout << result[i][j] << " ";
      }
      cout << endl;
  }

  return 0;
}
Output:-
Addition Result:
6 8
10 12
Subtraction Result:
-4 -4
-4 -4

(c.) Python Code

matrix1 = [[1, 2], [3, 4]]
matrix2 = [[5, 6], [7, 8]]
result_add = [[0, 0], [0, 0]]
result_sub = [[0, 0], [0, 0]]

# Addition
print("Addition Result:")
for i in range(2):
  for j in range(2):
      result_add[i][j] = matrix1[i][j] + matrix2[i][j]
      print(result_add[i][j], end=" ")
  print()

# Subtraction
print("Subtraction Result:")
for i in range(2):
  for j in range(2):
      result_sub[i][j] = matrix1[i][j] - matrix2[i][j]
      print(result_sub[i][j], end=" ")
  print()
Output:-
Addition Result:
6 8
10 12
Subtraction Result:
-4 -4
-4 -4

(d.) Java Code

public class Main {
public static void main(String[] args) {
    int[][] matrix1 = {{1, 2}, {3, 4}};
    int[][] matrix2 = {{5, 6}, {7, 8}};
    int[][] resultAdd = new int[2][2];
    int[][] resultSub = new int[2][2];

    // Addition
    System.out.println("Addition Result:");
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 2; ++j) {
            resultAdd[i][j] = matrix1[i][j] + matrix2[i][j];
            System.out.print(resultAdd[i][j] + " ");
        }
        System.out.println();
    }

    // Subtraction
    System.out.println("Subtraction Result:");
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 2; ++j) {
            resultSub[i][j] = matrix1[i][j] - matrix2[i][j];
            System.out.print(resultSub[i][j] + " ");
        }
        System.out.println();
    }
}
}
Output:-
Addition Result:
6 8
10 12
Subtraction Result:
-4 -4
-4 -4