C program to print matrix multiply.

 

C program to print matrix multiply.

 
 #include <stdio.h>


#define M 100
#define N 100
//creating a matrix
void matrix(int mat[M][N],int m, int n){
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            scanf("%d",&mat[i][j]);
        } 
    }
}

void matMul(int mat1[M][N],int mat2[M][N],int result[M][N], int m, int n, int p){
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            result[i][j]=0;
            for (int k = 0; k < p; k++)
            {
                result[i][j]+=mat1[i][k]*mat2[k][j];
            }  
        }  
    }
}
//function to display matrix   
void displayMatrix(int mat[M][N],int m, int n){
    printf("Matrix multiplication of given two matrix:\n");
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            printf("%d ",mat[i][j]);
        }
        printf("\n");   
    } 
}
int main(){

    int mat1[M][N],mat2[M][N], result[M][N];
    int m,n, o, p;
    printf("Enter the size of first  matrix :");
    scanf("%d %d", &m, &n);
    printf("Enter the size of second  matrix :");
    scanf("%d %d", &o, &p);

    if (n!=o)
    {
        printf("Matrix multiplication is not possible:");
        return 0;
    }else
    {
    printf("Enter element of first matrix: ");    
    matrix(mat1, m,n);
    printf("Enter element of Second matrix: ");    
    matrix(mat2, o,p);
    matMul(mat1,mat2, result,m,n,p);
    displayMatrix(result,m, p);
    }
    getch();

    return 0;
} 
 

OUTPUT: