C Program for Multiplication of Matrix using Recursion

C Program for Multiplication of Matrix using Recursion:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>

void matrixMult(int[10][10],int[10][10]);  //Prototyping
int row1,row2,col1,col2;
int res[10][10];

main()
{
   int matr1[10][10], matr2[10][10],i,j,k;

   printf("Enter the row and column of first matrix: ");
   scanf("%d%d",&row1,&col1);
   printf("\nEnter the row and column of second matrix: ");
   scanf("%d%d",&row2,&col2);

   if (col1 != row2)
    {
      printf("Matrix multiplication is not possible");
    }
   else
    {
      printf("Enter the First matrix: ");
      for(i=0;i<row1;i++)
      {
         for(j=0;j<col1;j++)
         {
            scanf("%d",&matr1[i][j]);
         }
      }

      printf("Enter the Second matrix: ");
        for(i=0;i<row2;i++)
        {
         for(j=0;j<col2;j++)
         {
            scanf("%d",&matr2[i][j]);
         }
        }

      //Printing first matrix
      printf("\nThe First matrix is: ");
      for(i=0;i<row1;i++)
      {
         printf("\n");
         for(j=0;j<col1;j++)
         {
            printf("%d ",matr1[i][j]);
         }
      }

      //Printing second matrix
      printf("\nThe Second matrix is: n");
      for(i=0;i<row2;i++)
      {
         printf("\n");
         for(j=0;j<col2;j++)
         {
            printf("%d ", matr2[i][j]);
         }
      }
      matrixMult(matr1,matr2);
   }

   printf("\nThe multiplication of two matrixes is : \n");
   for(i=0;i<row1;i++)
    {
      printf("\n");
      for(j=0;j<col2;j++)
      {
         printf("%d ",res[i][j]);
      }
   }
}

void matrixMult(int a[10][10],int b[10][10])
{
   static int sum,i=0,j=0,k=0;

   if(i<row1)
    {
      if(j<col2)
      {
         if(k<col1)
         {
            sum=sum+a[i][k]*b[k][j];
            k++;
            matrixMult(a,b);
         }
         res[i][j]=sum;
         sum=0;
         k=0;
         j++;
         matrixMult(a,b);
      }
      j=0;
      i++;
      matrixMult(a,b);
    }
}

OUTPUT:

Enter the row and column of first matrix: 3 3

Enter the row and column of second matrix: 3 3
Enter the First matrix:
1 4 3
1 5 9
3 2 7
Enter the Second matrix:
3 6 7
1 5 2
1 7 5

The First matrix is:
1 4 3
1 5 9
3 2 7
The Second matrix is: n
3 6 7
1 5 2
1 7 5
The multiplication of two matrixes is :

10 47 30
17 94 62
18 77 60

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.