C C++ program to find multiplication of two matrix

Coding in C

#include<stdio.h>
int main()
{
    int a[3][3],b[3][3],r[3][3],i,j,k;
    printf("Enter Values of Matrix 1:\n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    printf("Enter Values of Matrix 2:\n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            scanf("%d",&b[i][j]);
        }
    }
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            r[i][j]=0;
            for(k=0;k<3;k++)
            {
                r[i][j]=r[i][j]+a[i][k]*b[k][j];
            }
        }
    }
    printf("Multiplication of Matrix:\n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            printf("%d\t",r[i][j]);
        }
        printf("\n");
    }
    return 0;
 }

Coding in C++

#include<iostream>
using namespace std;
int main()
{
    int a[3][3],b[3][3],r[3][3],i,j,k;
    cout<<"Enter Values of Matrix 1 :\n";
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            cin>>a[i][j];
        }
    }
    cout<<"Enter Values of Matrix 2 :\n";
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            cin>>b[i][j];
        }
    }
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            r[i][j]=0;
            for(k=0;k<3;k++)
            {
                r[i][j]=r[i][j]+a[i][k]*b[k][j];
            }
        }
    }
    cout<<"Multiplication of Matrix:\n";
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            cout<<r[i][j]<<"\t";
        }
        cout<<"\n";
    }
    return 0;
 }

Output

C C++ program to find multiplication of two matrix

Published by

admin

I am a computer teacher, programmer and web developer