Matrix Multiplication using Recursion in C

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 display a linked list in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Perform Matrix Multiplication using Recursion
  3.  */
  4. #include <stdio.h>
  5.  
  6. void multiply(int, int, int [][10], int, int, int [][10], int [][10]);
  7. void display(int, int, int[][10]);
  8.  
  9. int main()
  10. {
  11.     int a[10][10], b[10][10], c[10][10] = {0};
  12.     int m1, n1, m2, n2, i, j, k;
  13.  
  14.     printf("Enter rows and columns for Matrix A respectively: ");
  15.     scanf("%d%d", &m1, &n1);
  16.     printf("Enter rows and columns for Matrix B respectively: ");
  17.     scanf("%d%d", &m2, &n2);
  18.     if (n1 != m2)
  19.     {
  20.         printf("Matrix multiplication not possible.\n");
  21.     }
  22.     else
  23.     {
  24.         printf("Enter elements in Matrix A:\n");
  25.         for (i = 0; i < m1; i++)
  26.         for (j = 0; j < n1; j++)
  27.         {
  28.             scanf("%d", &a[i][j]);
  29.         }
  30.         printf("\nEnter elements in Matrix B:\n");
  31.         for (i = 0; i < m2; i++)
  32.         for (j = 0; j < n2; j++)
  33.         {
  34.             scanf("%d", &b[i][j]);
  35.         }
  36.         multiply(m1, n1, a, m2, n2, b, c);
  37.     }
  38.     printf("On matrix multiplication of A and B the result is:\n");
  39.     display(m1, n2, c);
  40. }
  41.  
  42. void multiply (int m1, int n1, int a[10][10], int m2, int n2, int b[10][10], int c[10][10])
  43. {
  44.     static int i = 0, j = 0, k = 0;
  45.  
  46.     if (i >= m1)
  47.     {
  48.         return;
  49.     }
  50.     else if (i < m1)
  51.     {
  52.         if (j < n2)
  53.         {
  54.             if (k < n1)
  55.             {
  56.                 c[i][j] += a[i][k] * b[k][j];
  57.                 k++;
  58.                 multiply(m1, n1, a, m2, n2, b, c);
  59.             }
  60.             k = 0;
  61.             j++;
  62.             multiply(m1, n1, a, m2, n2, b, c);
  63.         }
  64.         j = 0;
  65.         i++;
  66.         multiply(m1, n1, a, m2, n2, b, c);
  67.     }
  68. }
  69.  
  70. void display(int m1, int n2, int c[10][10])
  71. {
  72.     int i, j;
  73.  
  74.     for (i = 0; i < m1; i++)
  75.     {
  76.         for (j = 0; j < n2; j++)
  77.         {
  78.             printf("%d  ", c[i][j]);
  79.         }
  80.         printf("\n");
  81.     }
  82. }

$ cc pgm23.c
$ a.out
Enter rows and columns for Matrix A respectively: 2
2
Enter rows and columns for Matrix B respectively: 2
2
Enter elements in Matrix A:
12 56
45 78
 
Enter elements in Matrix B:
2 6
5 8
On matrix multiplication of A and B the result is:
304  520  
480  894

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Matrix, go to C Programming Examples on Matrix. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.