Answers for "multiplication of two matrices in c"

C
2

multiply matrix in c

#include <stdio.h>

void main ()
{
    int i, j, k, m1, n1, m2, n2;
    int matrix1[10][10],matrix2[10][10],mult[10][10];

    printf("Enter number of rows of matrix 1 : ");
    scanf("%d", &m1);
    printf("Enter number of columns of matrix 1 : ");
    scanf("%d", &n1);

    printf("n");

    for (i = 0; i < m1; i++)
    {
        for (j = 0; j < n1; j++)
        {
            printf("Enter element of matrix 1[%d][%d]: ", i, j);
            scanf("%d", &matrix1[i][j]);
        }
    }

    printf("n");

    printf("Enter number of rows of matrix 2 : ");
    scanf("%d", &m2);
    printf("Enter number of columns of matrix 2 : ");
    scanf("%d", &n2);

    printf("n");

    if(m1==n2)
    {
        for (i = 0; i < m2; i++)
        {
            for (j = 0; j < n2; j++)
            {
                printf("Enter elements of matrix 2[%d][%d]: ", i, j);
                scanf("%d", &matrix2[i][j]);
            }
        }

        printf("n");

        printf("n....Your resultant matrix is....nn");

       for(i=0;i<m1;i++)
        {
            for(j=0;j<n2;j++)
            {
                mult[i][j]=0;
                for(k=0;k<m2;k++)
                {
                    mult[i][j]+=matrix1[i][k]*matrix2[k][j];
                }
            }
        }

        for (i = 0; i < m1; i++)
        {
            for (j = 0; j < n2; j++)
            {
                printf("%dt", mult[i][j]);
            }
            printf("n");
        }

    }

    else
        printf("Matrix multiplication not possible");
}
Posted by: Guest on June-25-2021
0

matrix multiplication in c

#include <stdio.h>

int main()
{   int a_rows,a_cols,b_rows,b_cols,i,j,k,sum=0;

    printf("Enter the rows and columns for first matrix (row)(col)  :n");
    scanf("%d %d",&a_rows,&a_cols);

    printf("Enter the rows and columns for second matrix (row)(col) :n");
    scanf("%d %d",&b_rows,&b_cols);
    int a[a_rows][a_cols], b[b_rows][b_cols],matrix[10][10];

    if(a_cols != b_rows){
        printf("Sorry! We can't multiply the matrix because the column number of matrix 1 and the row number of matrix 2 aren't same !!n");
        
    }else{
        printf("Enter elements for first matrix  :n");
       for(i = 0; i < a_rows; i++){
           for(j = 0; j< a_cols; j++){
               scanf("%d",&a[i][j]);
           }
       }

    printf("Enter elements for second matrix  :n");

       for(i = 0; i < b_rows; i++){
           for(j = 0; j < b_cols; j++){
               scanf("%d",&b[i][j]);
           }
       }

       printf("multiplying matrix....n");
       //multiplying matrix
       for(i=0; i < a_rows; i++){
           for(j = 0; j < b_cols; j++){
               for(k = 0; k < a_cols; k++){
                   sum+=a[i][k] * b[k][j];
               }
               matrix[i][j] = sum;
               sum=0;
               
           }
           printf("n");
       }

       printf("first matrix  :n");
       for(i = 0; i < a_rows; i++){
           for(j = 0; j< a_cols; j++){
               printf("%d ",a[i][j]);
           }
           printf("n");
       }


        printf("nn");

       printf("second matrix  :n");
       for(i = 0; i < b_rows; i++){
           for(j = 0; j< b_cols; j++){
               printf("%d ",b[i][j]);
           }
           printf("n");
       }


       printf("Multiplied matrix  :n");
       for(i = 0; i < a_rows; i++){
           for(j = 0; j< b_cols; j++){
               printf("%d ",matrix[i][j]);
           }
           printf("n");
       }
    }

     

    return 0;
}
Posted by: Guest on July-19-2021

Code answers related to "multiplication of two matrices in c"

Code answers related to "C"

Browse Popular Code Answers by Language