Monday, November 28, 2016

A C Program to Calculate the Multiplication of Two Matrices


/*
Program Name: Write a program to calculate the multiplication
of two matrices by using for loop in C.
Program By: Elliyas Ahmed
Date: 27.11.2016
*/

#include <stdio.h>
int main()
{
    int matA[2][2], matB[2][3],i,j;

    /*================MATRIX INPUT START==================*/

  /*Input the elements of Matrix A*/

    printf("Input Element of 2X2 dimensional matA\n");

    for(i=1; i<=2; i++)
    {
        for(j=1; j<=2; j++)
        {
            printf("matA%d%d = ",i,j);
            scanf("%d",&matA[i][j]);
        }
    }

    /*Input the elements of Matrix B*/

    printf("Input Element of 3X2 dimensional matB\n");

    for(i=1; i<=2; i++)
    {
        for(j=1; j<=3; j++)
        {
            printf("matB%d%d = ",i,j);
            scanf("%d",&matB[i][j]);
        }
    }
/*================MATRIX INPUT END==================*/

  
/*================CALCULATION=========================*/
    for(i=1; i<=2; i++)
    {
        for(j=1; j<=3; j++)
        {
            matA[i][1] * matB[1][j];
        }
    }

    for(i=1; i<=2; i++)
    {
        for(j=1; j<=3; j++)
        {
            matA[i][2] * matB[2][j];
        }
    }

/*================FINAL CALCULATION AND PRINT RESULT=========================*/

    printf("\nMultiplication of MAT A AND MAT B\n\n");

    for(i=1; i<=2; i++)
    {
        for(j=1; j<=3; j++)
        {
            printf("%d\t",(matA[i][1] * matB[1][j]) + (matA[i][2] * matB[2][j]) );
        }
        printf("\n");
    }
    printf("\n\n");

    return 0;
}

No comments:

Post a Comment

We appreciate your comment. Shout here.