**Matrix Multiplication using Recursion**

**Matrix Multiplication using Recursion**

Write a C Program to implement Matrix Multiplication using Recursion. Here’s simple Program to implement Matrix Multiplication using Recursion in C Programming Language.

**Recursion : :**

**Recursion : :**

- Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a
of the function.**recursive call**

- The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.

- Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.

**Problem : :**

**Problem : :**

The following C program, using recursion, performs Matrix multiplication of two matrices and displays the result. We use 2 D array to represent a matrix and resulting matrix is stored in a different matrix.

Here is the source code of the C Program to implement Matrix Multiplication using Recursion. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.

**SOURCE CODE : :**

**SOURCE CODE : :**

/* C Program to implement Matrix Multiplication using Recursion */ #include <stdio.h> void multiply(int, int, int [][10], int, int, int [][10], int [][10]); void display(int, int, int[][10]); int main() { int a[10][10], b[10][10], c[10][10] = {0}; int m1, n1, m2, n2, i, j, k; printf("Enter rows for Matrix A :: "); scanf("%d", &m1); printf("\nEnter cols for Matrix A :: "); scanf("%d", &n1); printf("\nEnter rows for Matrix B :: "); scanf("%d", &m2); printf("\nEnter cols for Matrix B :: "); scanf("%d", &n2); if (n1 != m2) { printf("\nMatrix multiplication not possible.\n"); } else { printf("\nEnter elements in Matrix A :: \n"); for (i = 0; i < m1; i++) for (j = 0; j < n1; j++) { printf("\nEnter A[%d][%d] element :: ",i,j); scanf("%d", &a[i][j]); } printf("\nEnter elements in Matrix B :: \n"); for (i = 0; i < m1; i++) for (j = 0; j < n1; j++) { printf("\nEnter B[%d][%d] element :: ",i,j); scanf("%d", &a[i][j]); } multiply(m1, n1, a, m2, n2, b, c); } printf("\nAfter matrix multiplication of A and B, The result is A * B = C :: \n"); printf("\nElements in Matrix C are :: \n"); display(m1, n2, c); return 0; } void multiply (int m1, int n1, int a[10][10], int m2, int n2, int b[10][10], int c[10][10]) { static int i = 0, j = 0, k = 0; if (i >= m1) { return; } else if (i < m1) { if (j < n2) { if (k < n1) { c[i][j] += a[i][k] * b[k][j]; k++; multiply(m1, n1, a, m2, n2, b, c); } k = 0; j++; multiply(m1, n1, a, m2, n2, b, c); } j = 0; i++; multiply(m1, n1, a, m2, n2, b, c); } } void display(int m1, int n2, int c[10][10]) { int i, j; for (i = 0; i < m1; i++) { for (j = 0; j < n2; j++) { printf(" %d ", c[i][j]); } printf("\n"); } }

**Output : :**

**Output : :**

/* C Program to implement Matrix Multiplication using Recursion */ Enter rows for Matrix A :: 2 Enter cols for Matrix A :: 2 Enter rows for Matrix B :: 2 Enter cols for Matrix B :: 2 Enter elements in Matrix A :: Enter A[0][0] element :: 12 Enter A[0][1] element :: 56 Enter A[1][0] element :: 45 Enter A[1][1] element :: 78 Enter elements in Matrix B :: Enter B[0][0] element :: 2 Enter B[0][1] element :: 6 Enter B[1][0] element :: 5 Enter B[1][1] element :: 8 After matrix multiplication of A and B, The result is A * B = C :: Elements in Matrix C are :: 304 520 480 894 Process returned 0

Above is the source code for C Program to implement Matrix Multiplication using Recursion which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may * Contact Us* through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.

**Thanks for reading the post….**