×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C program for matrix multiplication using recursion

Here, we are going to learn how to perform matrix multiplication operation using recursion in C programming language?
Submitted by Nidhi, on July 13, 2021

Problem statement

Given two matrices, we have to find their multiplication using the recursion.

Matrix multiplication using recursion

The source code to perform matrix multiplication using recursion is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

C program for matrix multiplication using recursion

// C program to perform matrix multiplication using recursion.

#include <stdio.h>

void MatrixMultiplication(int row1, int col1, int matrix1[2][2], int row2,
                          int col2, int matrix2[2][2], int matrix3[2][2]) {
  static int i = 0;
  static int j = 0;
  static int k = 0;

  if (i >= row1) return;

  if (i < row1) {
    if (j < col2) {
      if (k < col1) {
        matrix3[i][j] += matrix1[i][k] * matrix2[k][j];
        k++;

        MatrixMultiplication(row1, col1, matrix1, row2, col2, matrix2, matrix3);
      }

      k = 0;
      j++;

      MatrixMultiplication(row1, col1, matrix1, row2, col2, matrix2, matrix3);
    }

    j = 0;
    i++;
    MatrixMultiplication(row1, col1, matrix1, row2, col2, matrix2, matrix3);
  }
}

int main() {
  int arr1[2][2] = {{1, 2}, {3, 4}};

  int arr2[2][2] = {{2, 3}, {4, 5}};

  int arr3[2][2] = {0};

  int i, j;

  printf("First Matrix :\n");
  for (i = 0; i < 2; i++) {
    for (j = 0; j < 2; j++) printf("%d ", arr1[i][j]);
    printf("\n");
  }

  printf("\nSecond Matrix :\n");
  for (i = 0; i < 2; i++) {
    for (j = 0; j < 2; j++) printf("%d ", arr2[i][j]);
    printf("\n");
  }

  MatrixMultiplication(2, 2, arr1, 2, 2, arr2, arr3);

  printf("\nMatrix multiplication:\n");
  for (i = 0; i < 2; i++) {
    for (j = 0; j < 2; j++) printf("%d ", arr3[i][j]);
    printf("\n");
  }

  printf("\n");

  return 0;
}

Output

First Matrix :
1 2 
3 4 

Second Matrix :
2 3 
4 5 

Matrix multiplication:
10 13 
22 29 

Explanation

In the above program, we created two functions MatrixMultiplication() and main() function. The MatrixMultiplication() function is a user-defined function, it is used to perform the multiplication of two matrices using recursion.

In the main() function, we created three matrices arr1, arr2, arr3. Then we perform the multiplication of two matrices using the MatrixMultiplication() function and then we printed the result on the console screen.

C Two-dimensional Arrays Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.