Matrix multiplication using recursion in c





C code to multiply two matrix by recursion:

#include<stdio.h>
#define MAX 10

void multiplyMatrix(int [MAX][MAX],int [MAX][MAX]);
int m,n,o,p;
int c[MAX][MAX];

int main(){
   
    int a[MAX][MAX],b[MAX][MAX],i,j,k;

    printf("Enter the row and column of first matrix: ");
    scanf("%d %d",&m,&n);
    printf("Enter the row and column of second matrix: ");
    scanf("%d %d",&o,&p);

    if(n!=o){

         printf("Matrix multiplication is not possible");
         printf("\nColumn of first matrix must be same as row of second matrix");
    }
  else{

      printf("Enter the First matrix: ");
      for(i=0;i<m;i++)
      for(j=0;j<n;j++)
           scanf("%d",&a[i][j]);

      printf("Enter the Second matrix: ");
      for(i=0;i<o;i++)
      for(j=0;j<p;j++)
           scanf("%d",&b[i][j]);

      printf("\nThe First matrix is: \n");
      for(i=0;i<m;i++){
      printf("\n");
      for(j=0;j<n;j++){
           printf("%d\t",a[i][j]);
      }
      }

      printf("\nThe Second matrix is: \n");
      for(i=0;i<o;i++){
      printf("\n");
      for(j=0;j<p;j++){
           printf("%d\t",b[i][j]);
      }
      }

      multiplyMatrix(a,b);

  }

  printf("\nThe multiplication of two matrix is: \n");
  for(i=0;i<m;i++){
      printf("\n");
      for(j=0;j<p;j++){
           printf("%d\t",c[i][j]);
      }
  }
  return 0;
}

void multiplyMatrix(int a[MAX][MAX],int b[MAX][MAX]){

    static int sum,i=0,j=0,k=0;

    if(i<m){ //row of first matrix
    if(j<p){  //column of second matrix
         if(k<n){
             sum=sum+a[i][k]*b[k][j];
             k++;
             multiplyMatrix(a,b);
         }
         c[i][j]=sum;
             sum=0;
             k=0;
             j++;
             multiplyMatrix(a,b);
    }
    j=0;
    i++;
    multiplyMatrix(a,b);
    }
}

Sample output:

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

The First matrix is:

1       2
3       4
The Second matrix is:

2       3
4       5
The multiplication of two matrix is:

10      13
22      29

C code to multiply two matrix without using recursion:

#include<stdio.h>
#define MAX 10

void multiplyMatrix(int [MAX][MAX],int [MAX][MAX]);
int m,n,o,p;
int c[MAX][MAX];

int main(){
   
    int a[MAX][MAX],b[MAX][MAX],i,j,k;

    printf("Enter the row and column of first matrix: ");
    scanf("%d %d",&m,&n);
    printf("Enter the row and column of second matrix: ");
    scanf("%d %d",&o,&p);

    if(n!=o){
         printf("Matrix multiplication is not possible");
         printf("\column of first matrix must be same as row of second matrix");
    }
    else{
        
         printf("Enter the First matrix: ");
         for(i=0;i<m;i++)
             for(j=0;j<n;j++)
                 scanf("%d",&a[i][j]);

         printf("Enter the Second matrix: ");
         for(i=0;i<o;i++)
             for(j=0;j<p;j++)
                 scanf("%d",&b[i][j]);

         printf("\nThe First matrix is: \n");
         for(i=0;i<m;i++){
             printf("\n");
             for(j=0;j<n;j++){
                 printf("%d\t",a[i][j]);
             }
         }

         printf("\nThe Second matrix is: \n");
         for(i=0;i<o;i++){
             printf("\n");
             for(j=0;j<p;j++){
                 printf("%d\t",b[i][j]);
             }
         }

         multiplyMatrix(a,b);
    }

    printf("\nThe multiplication of two matrix is: \n");
    for(i=0;i<m;i++){
         printf("\n");
         for(j=0;j<p;j++){
           printf("%d\t",c[i][j]);
         }
    }
    return 0;
}

void multiplyMatrix(int a[MAX][MAX],int b[MAX][MAX]){

    int sum,i=0,j=0,k=0;

    while(i<m){ //row of first matrix
    j=0;
    while(j<p){  //column of second matrix
         k=0;
         sum=0;
         while(k<n){
               sum=sum+a[i][k]*b[k][j];
                k++;
            }
           c[i][j]=sum;
            j++;
    }
    i++;
    }
}





Sum of n numbers using recursion in c
Matrix multiplication using recursion in c
Multiplication using recursion in c
Lcm using recursion in c
Using recursion in c find the largest element in an array
Prime number program in c using recursion
Decimal to binary conversion in c using recursion
C program for fibonacci series using recursion
Reverse a string using recursion
Write a program for palindrome using recursion
Find factorial of a number using recursion in c program
Find gcd of a number using recursion in c program
Find sum of digits of a number using recursion using cprogram
Find power of a number using recursion using c program
Binary search through recurssion using c program
Reverse a number using recursion in c program
Big list of c program examples
Post a Comment