Friday, 24 October 2014

Multiplication of Two Matrices

Non-Recursive C Program:

Aim: Two print matrix multiplication.

Program:
#include <stdio.h>
#include <conio.h>
void main()
  { 
   int A[10][10], B[10][10],mat[10][10], i, j,k, R1,C1,R2,C2;
clrscr();
printf("Enter the order of the matrice A \n");
scanf("%d %d", &R1, &C1);
printf("Enter the order of the matrice B \n");
scanf("%d %d", &R2, &C2);
if(C1==R2)
{
printf("Enter the elements of  matrix A\n");
for(i=0; i<R1; i++)
   {   for(j=0; j<C1; j++)
       {
          scanf("%d",&A[i][j]);
       }
   }

printf("MATRIX A is\n");
   for(i=0; i<R1; i++)
     { for(j=0; j<C1; j++)
          {
              printf("%3d",A[i][j]);
           }
        printf("\n");
     }

printf("Enter the elements of matrix B\n");
   for(i=0; i<R2; i++)
     {
        for(j=0; j<C2; j++)
         {
             scanf("%d",&B[i][j]);
          }
     }

  printf("MATRIX B is\n");
    for(i=0; i<R2; i++)
      {
        for(j=0; j<C2; j++)
          {
             printf("%3d",B[i][j]);
          }
          printf("\n");
      }

for(i=0; i< C1 ; i++)
   {
      for ( j=0; j < R2; j++)
         {
              mat[i][j]= 0 ;
        for (k=0; k < R2; k++)
          {
          mat[i][j]+ = A[i][k] * B[k] [j];
          }
        }
    }

printf("Mul matrix is\n");
   for(i=0; i<C1; i++)
      {
        for(j=0; j<R2; j++)
           {
              printf("%3d",sumat[i][j]) ;
            }
      printf("\n");
      }
}
else
  {
  printf("\nMatrice Multiplication is not possible");
   }
}

Click here to view matrix related programs.

No comments:

Post a Comment