Saturday 21 September 2013

Saturday 21 September 2013

Matrix multiplication in c


C program for multiplication of two matrices:

#include<stdio.h>
int main(){
  int a[3][3],b[3][3],c[3][3],i,j;
  printf("Enter the First matrix->");
  for(i=0;i<3;i++)
      for(j=0;j<3;j++)
           scanf("%d",&a[i][j]);
  printf("\nEnter the Second matrix->");
  for(i=0;i<3;i++)
      for(j=0;j<3;j++)
           scanf("%d",&b[i][j]);
  printf("\nThe First matrix is\n");
  for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
           printf("%d\t",a[i][j]);
  }
  printf("\nThe Second matrix is\n");
  for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
      printf("%d\t",b[i][j]);
   }
  
    for (i=0;i<3;i++)            //initialisation of mat mul
    {
        for (j=0;j<3;j++)
        {
             c[i][j]=0;
        }
    }
   for(i=0;i<3;i++){
       for(j=0;j<3;j++){
           int k;
       
            for (k=0;k<3;k++)
            {
                c[i][j]+=a[i][k]*b[k][j];
            }
       }}
   printf("\nmultiplication of matrix is\n");
   for(i=0;i<3;i++){
       printf("\n");
       for(j=0;j<3;j++)
            printf("%d\t",c[i][j]);
   }
   return 0;
}


Enter the First matrix->
Enter the Second matrix->
The First matrix is

1 2 3 
4 5 6 
2 4 6 
The Second matrix is

8 1 2 
3 4 5 
6 1 2 
The Multiplication of two matrix is

32 12 18 
83 30 45 
64 24 36 


No comments:

Post a Comment